advent-of-code/2025-python/output/day_09.py
2025-12-11 23:36:38 +01:00

99 lines
2.1 KiB
Python

from math import inf
from itertools import combinations
from output import ints
def solve(data):
p1 = 0
p2 = 0
A = [tuple(ints(pos)) for pos in data.splitlines()]
T = inf
R = 0
B = 0
L = inf
for r, c in A:
T = min(r, T)
R = max(c, R)
B = max(r, B)
L = min(c, L)
S = A[0]
V = set()
W = set()
while True:
V.add(S)
W.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):
W.add((r, c))
if E == A[0]:
break
S = E
V = V | W
def _within(a, b):
y1, x1 = a
y2, x2 = b
T = min(y1, y2)
R = max(x1, x2)
B = max(y1, y2)
L = min(x1, x2)
return all([
sum((r, L + 1) in W for r in range(T + 1, B)) % 2 == 0,
sum((r, R - 1) in W for r in range(T + 1, B)) % 2 == 0,
sum((T + 1, c) in W for c in range(L + 1, R)) % 2 == 0,
sum((B - 1, c) in W for c in range(L + 1, R)) % 2 == 0,
])
S = len(list(combinations(A, r=2)))
i = 0
for a, b in combinations(A, r=2):
i += 1
if i % 100 == 0:
print(f"{str(i):>6} / {S}")
y1, x1 = a
y2, x2 = b
x = abs(x1 - x2) + 1
y = abs(y1 - y2) + 1
p1 = max(p1, x * y)
if _within(a, b):
p2 = max(p2, max(p2, x * y))
return p1, p2
if __name__ == "__main__":
with open("./input/09.txt", "r") as f:
inp = f.read().strip()
in_p = """
7,1
11,1
11,7
9,7
9,5
2,5
2,3
7,3 """.strip()
p1, p2 = solve(inp)
print(p1)
print(p2)
assert p1 == 50
assert p2 == 24
# assert p1 == 4749838800
# assert p2 == 1624057680