From fbe6994e3ac4a439e7a4d08c69de6d82d15a9601 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Tue, 17 Dec 2024 00:17:21 +0100 Subject: [PATCH] Solve 2024:16 pt1 "Reindeer Maze" --- 2024-python/output/__init__.py | 2 +- 2024-python/output/day_16.py | 85 ++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 2024-python/output/day_16.py diff --git a/2024-python/output/__init__.py b/2024-python/output/__init__.py index 4ad56dc..0f69f41 100644 --- a/2024-python/output/__init__.py +++ b/2024-python/output/__init__.py @@ -69,7 +69,7 @@ def mdbg(m): def vdbg(seen, h, w): """Print-debug visited positions of a matrix""" for r in range(h): - print("".join(["#" if (r, c) in seen else "." for c in range(w)])) + print("".join(["#" if (r, c) in seen else " " for c in range(w)])) def cw(y, x): diff --git a/2024-python/output/day_16.py b/2024-python/output/day_16.py new file mode 100644 index 0000000..781c902 --- /dev/null +++ b/2024-python/output/day_16.py @@ -0,0 +1,85 @@ +import re +from collections import Counter, defaultdict, deque +from heapq import heappop, heappush +from itertools import chain, combinations, compress, permutations + +from output import ADJ, DD, D, ccw, cw, ints, matrix, mdbg, mhd, vdbg + + +def solve(data): + grid, H, W = matrix(data) + S = [(r, c) for r in range(H) for c in range(W) if grid[r][c] == "S"][0] + E = [(r, c) for r in range(H) for c in range(W) if grid[r][c] == "E"][0] + p1 = float('inf') + Q = deque([(0, (S,), 1)]) + seen = set() + lowest = [] + while Q: + cost, path, facing = heappop(Q) + pos = path[-1] + if (pos, facing) in seen: + continue + seen.add((pos, facing)) + if pos == E: + p1 = min(cost, p1) + lowest.append(path) + r, c = pos + for d, delta in enumerate(D): + dr, dc = delta + if grid[r + dr][c + dc] == "#": + continue + if abs(facing - d) == 2: + continue + if d != facing: + heappush(Q, (cost + 1000, path, d)) + else: + heappush(Q, (cost + 1, path + ((r + dr, c + dc),), d)) + return p1, None + + +if __name__ == "__main__": + import os + + # use dummy data + inp = """ + ################# + #...#...#...#..E# + #.#.#.#.#.#.#.#.# + #.#.#.#...#...#.# + #.#.#.#.###.#.#.# + #...#.#.#.....#.# + #.#.#.#.#.#####.# + #.#...#.#.#.....# + #.#.#####.#.###.# + #.#.#.......#...# + #.#.###.#####.### + #.#.#...#.....#.# + #.#.#.#####.###.# + #.#.#.........#.# + #.#.#.#########.# + #S#.............# + ################# + + + + """.strip() + + # uncomment to instead use stdin + # import sys; inp = sys.stdin.read().strip() + + # uncomment to use AoC provided puzzle input + with open("./input/16.txt", "r") as f: + inp = f.read().strip() + + # uncomment to do initial data processing shared by part 1-2 + p1, p2 = solve(inp) + + print(p1) + os.system(f"echo {p1} | wl-copy") + print(p2) + os.system(f"echo {p2} | wl-copy") + + # uncomment and replace 0 with actual output to refactor code + # and ensure nonbreaking changes + # assert p1 == 0 + # assert p2 == 0