Solve 2025 day 8 pt 1-2

For the First time this season, pt 2 was not an epic
HOLY SHIIIT!!! facemelter.

I stared at my code in disbelief during pt 1 for several
minutes before I increased the test point from from 40
to 1000 and got the right answer.
This commit is contained in:
Anders Englöf Ytterström 2025-12-08 07:56:36 +01:00
parent ee8d11f84d
commit 8fbd267d99

View file

@ -0,0 +1,58 @@
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