From f971917640d4472c98f642aa09ce65b4e7257faa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Tue, 9 Dec 2025 14:49:19 +0100 Subject: [PATCH] Solve 2025 day 9 pt 1 Pt 2 is almost there. Need a better flood fill algorithm. --- 2025-python/output/day_09.py | 95 ++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 2025-python/output/day_09.py diff --git a/2025-python/output/day_09.py b/2025-python/output/day_09.py new file mode 100644 index 0000000..844d437 --- /dev/null +++ b/2025-python/output/day_09.py @@ -0,0 +1,95 @@ +from itertools import combinations + +from output import ints + + +def solve(data): + p1 = 0 + p2 = 0 + A = [tuple(ints(pos)) for pos in data.splitlines()] + + S = A[0] + V = set() + B = set() + + while True: + V.add(S) + y, x = S + he = [(r, c) for r, c in A if r == y and (r, c) not in V] + ve = [(r, c) for r, c in A if c == x and (r, c) not in V] + if not he and not ve: + E = A[0] + elif he and ve: + E = min(min(he), min(ve)) + elif he and not ve: + E = min(he) + else: + E = min(ve) + y2, x2 = E + for r in range(min(y, y2), max(y, y2) + 1): + for c in range(min(x, x2), max(x, x2) + 1): + B.add((r, c)) + if E == A[0]: + break + S = E + # find out a better start for Flood fill by visualizing + Y = 1841 + X = 53478 + # or, find a way to floodfill without BFS + # end + y, x = Y, X + Q = [(y, x)] + V = V | B + while Q: + yx = Q.pop() + if yx in V: + continue + V.add(yx) + y, x = yx + for dy, dx in D: + Q.append((dy + y, dx + x)) + for a, b in combinations(A, r=2): + y1, x1 = a + y2, x2 = b + x = abs(x1 - x2) + 1 + y = abs(y1 - y2) + 1 + p1 = max(p1, x * y) + if ( + len( + set( + [ + (min(y1, y2), min(x1, x2)), + (min(y1, y2), max(x1, x2)), + (max(y1, y2), min(x1, x2)), + (max(y1, y2), max(x1, x2)), + ] + ) + - V + ) + == 0 + ): + p2 = max(p2, max(p2, x * y)) + return p1, p2 + + +if __name__ == "__main__": + inp = """ +7,1 +11,1 +11,7 +9,7 +9,5 +2,5 +2,3 +7,3 """.strip() + + with open("./input/09.txt", "r") as f: + inp = f.read().strip() + + p1, p2 = solve(inp) + + print(p1) + print(p2) + + assert p1 == 4749838800 + assert p2 == 1624057680