advent-of-code/2025-python/output/day_08.py

59 lines
1.3 KiB
Python
Raw Normal View History

from math import sqrt
from itertools import combinations
from output import ints
def solve(data):
Di = sorted(
map(_euclidan_distance, combinations(data.splitlines(), r=2)),
key=lambda r: r[1],
)
C = []
S = len(data.splitlines())
IP = 1000
for i, pq in enumerate(Di, start=1):
pq, _ = pq
c = set(pq)
p, q = pq
rmq = []
nothing = False
for j in C:
if p in j and q in j:
nothing = True
continue
if p in j or q in j:
c = c | j
rmq.append(j)
C = [e for e in C if e not in rmq]
if c == pq and nothing:
continue
if len(c) == S:
p2 = ints(p)[0] * ints(q)[0]
break
C = [c] + C
if i == IP:
a, b, c = sorted(list(map(len, C)), reverse=True)[:3]
p1 = a * b * c
return p1, p2
def _euclidan_distance(pq):
p, q = pq
p1, p2, p3 = ints(p)
q1, q2, q3 = ints(q)
return (p, q), sqrt((p1 - q1) ** 2 + (p2 - q2) ** 2 + (p3 - q3) ** 2)
if __name__ == "__main__":
with open("./input/08.txt", "r") as f:
inp = f.read().strip()
p1, p2 = solve(inp)
print(p1)
print(p2)
assert p1 == 84968
assert p2 == 8663467782