From ee8d11f84da91df5d8323f833ce1ed37f2a55e64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Sun, 7 Dec 2025 14:57:28 +0100 Subject: [PATCH] Solve 2025 day 7 pt 1-2 For part 1, BFS is used since there is no need to visit each splitter more than once to count visited splitters. For part 2, the code initially removed the visited check. This failed miserably, losing momentum aound Y=54-56. A recursive function with memoization solves it much faster. --- 2025-python/output/day_07.py | 50 ++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 2025-python/output/day_07.py diff --git a/2025-python/output/day_07.py b/2025-python/output/day_07.py new file mode 100644 index 0000000..22b6c25 --- /dev/null +++ b/2025-python/output/day_07.py @@ -0,0 +1,50 @@ +import functools +from output import grid + + +def solve(data): + G = grid(data, o="^") + p1 = set() + p2 = 0 + H = len(data.split()) + W = len(data.split()[0]) + S = (0, data.split()[0].index("S")) + Q = [S] + while Q: + y, x = Q.pop(0) + if y == H: + continue + if (y, x) in p1: + continue + if (y, x) in G: + Q.append((y, x - 1)) + Q.append((y, x + 1)) + p1.add((y, x)) + else: + Q.append((y + 1, x)) + p1 = len(p1) + + @functools.cache + def _timelines(p): + y, x = p + if not 0 <= y < H or not 0 <= x < W: + return 1 + if p in G: + return _timelines((y, x - 1)) + _timelines((y, x + 1)) + return _timelines((y + 1, x)) + + p2 = _timelines(S) + return p1, p2 + + +if __name__ == "__main__": + with open("./input/07.txt", "r") as f: + inp = f.read().strip() + + p1, p2 = solve(inp) + + print(p1) + print(p2) + + assert p1 == 1539 + assert p2 == 6479180385864