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