day8
This commit is contained in:
@@ -0,0 +1,62 @@
|
|||||||
|
"""Day 8: Advent of Code 2025."""
|
||||||
|
|
||||||
|
import time
|
||||||
|
from aoc import read_lines
|
||||||
|
import math
|
||||||
|
from functools import reduce
|
||||||
|
|
||||||
|
def distance(start: tuple[int,int,int], end: tuple[int,int,int]):
|
||||||
|
return math.sqrt(math.pow(start[0] - end[0], 2) + math.pow(start[1] - end[1], 2) + math.pow(start[2] - end[2], 2))
|
||||||
|
|
||||||
|
|
||||||
|
def part1(data):
|
||||||
|
paths = {}
|
||||||
|
i = 0
|
||||||
|
while i < len(data):
|
||||||
|
t = data[i]
|
||||||
|
min_dist, closest = min((distance(t, y), y) for y in data if y != t)
|
||||||
|
paths[tuple(t), tuple(closest)] = min_dist
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
sp = list(sorted(paths.items(), key=lambda x: x[1]))
|
||||||
|
circuits = []
|
||||||
|
for jb in sp[10:]:
|
||||||
|
t1, t2 = jb[0]
|
||||||
|
matching = [i for i, c in enumerate(circuits) if t1 in c or t2 in c]
|
||||||
|
if not matching:
|
||||||
|
circuits.append({t1, t2})
|
||||||
|
elif len(matching) == 1:
|
||||||
|
circuits[matching[0]].update({t1, t2})
|
||||||
|
else:
|
||||||
|
# Merga circuits
|
||||||
|
merged = {t1, t2}
|
||||||
|
for i in sorted(matching, reverse=True):
|
||||||
|
merged.update(circuits.pop(i))
|
||||||
|
circuits.append(merged)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def part2(data):
|
||||||
|
"""Solve part 2."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
DAY = 8 # <- ändra till rätt dag
|
||||||
|
data = read_lines(DAY)
|
||||||
|
array = [[int(x) for x in line.split(',')] for line in data]
|
||||||
|
|
||||||
|
|
||||||
|
t0 = time.perf_counter()
|
||||||
|
p1 = part1(array)
|
||||||
|
t1 = time.perf_counter()
|
||||||
|
print(f"Part 1: {p1} ({(t1-t0)*1000:.2f} ms)")
|
||||||
|
|
||||||
|
t0 = time.perf_counter()
|
||||||
|
p2 = part2(data)
|
||||||
|
t1 = time.perf_counter()
|
||||||
|
print(f"Part 2: {p2} ({(t1-t0)*1000:.2f} ms)")
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
162,817,812
|
||||||
|
57,618,57
|
||||||
|
906,360,560
|
||||||
|
592,479,940
|
||||||
|
352,342,300
|
||||||
|
466,668,158
|
||||||
|
542,29,236
|
||||||
|
431,825,988
|
||||||
|
739,650,466
|
||||||
|
52,470,668
|
||||||
|
216,146,977
|
||||||
|
819,987,18
|
||||||
|
117,168,530
|
||||||
|
805,96,715
|
||||||
|
346,949,466
|
||||||
|
970,615,88
|
||||||
|
941,993,340
|
||||||
|
862,61,35
|
||||||
|
984,92,344
|
||||||
|
425,690,689
|
||||||
Reference in New Issue
Block a user