diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-12-20 07:32:59 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-12-20 07:32:59 +0100 |
commit | ff03dafbae6f53027a1e79e1a60678afc31ebbdf (patch) | |
tree | a473afdc32ba12dcf411e38ec0075d2e135d6280 | |
parent | 1b1ee3db6b7cf6746782d9c2fb5f80bfa3f3afb5 (diff) |
Add 2024 day 20 solutions
-rw-r--r-- | 2024/20/.gitignore | 1 | ||||
-rw-r--r-- | 2024/20/Makefile | 1 | ||||
-rw-r--r-- | 2024/20/puzzles.py | 42 | ||||
-rw-r--r-- | Makefiles/py.mk | 10 |
4 files changed, 51 insertions, 3 deletions
diff --git a/2024/20/.gitignore b/2024/20/.gitignore new file mode 100644 index 0000000..8931d44 --- /dev/null +++ b/2024/20/.gitignore @@ -0,0 +1 @@ +puzzle-[12].py
\ No newline at end of file diff --git a/2024/20/Makefile b/2024/20/Makefile new file mode 100644 index 0000000..6e58a9d --- /dev/null +++ b/2024/20/Makefile @@ -0,0 +1 @@ +include ../../Makefiles/py.mk
\ No newline at end of file diff --git a/2024/20/puzzles.py b/2024/20/puzzles.py new file mode 100644 index 0000000..8727f6e --- /dev/null +++ b/2024/20/puzzles.py @@ -0,0 +1,42 @@ +#!/usr/bin/python3 + +def main() -> None: + points: set[complex] = set() + + with open("input", "r") as f: + for i, line in enumerate(f.readlines()): + for j, char in enumerate(line): + if char in "#\n": + continue + points.add(pos := complex(j, i)) + if char == 'S': + start = pos + + print(solve(start, points, 100, 2 if PUZZLE_PART == 1 else 20)) + +def solve( + pos: complex, + points: set[complex], + minsave: int, + maxcheat: int, +) -> int: + path: list[complex] = [] + while points: + path.append(pos) + for v⃗ in [1, -1, 1j, -1j]: + if (p := pos + v⃗) in points: + break + points.remove(pos) + pos = p + + cnt = 0 + for i, p1 in enumerate(path): + off = i + minsave + for j, p2 in enumerate(path[off + 2:], off + 2): + d = p1 - p2 + if abs(d.real) + abs(d.imag) <= min(j - off, maxcheat): + cnt += 1 + return cnt + +if __name__ == "__main__": + main()
\ No newline at end of file diff --git a/Makefiles/py.mk b/Makefiles/py.mk index e32992b..541f6f0 100644 --- a/Makefiles/py.mk +++ b/Makefiles/py.mk @@ -1,9 +1,13 @@ .POSIX: all: - sed '/START PART 2/,/END PART 2/d' puzzles.py >puzzle-1.py - sed '/START PART 1/,/END PART 1/d' puzzles.py >puzzle-2.py + sed -e '/START PART 2/,/END PART 2/d' \ + -e '1aPUZZLE_PART = 1' \ + puzzles.py >puzzle-1.py + sed -e '/START PART 1/,/END PART 1/d' \ + -e '1aPUZZLE_PART = 2' \ + puzzles.py >puzzle-2.py chmod +x puzzle-[12].py clean: - rm -f puzzle-[12].py + rm -f puzzle-[12].py
\ No newline at end of file |