My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add 2025/08

+38
+38
2025/day08.py
··· 1 + import fileinput 2 + from collections import Counter 3 + 4 + from utils import UnionFind 5 + 6 + 7 + # Read problem input. 8 + BOXES = [] 9 + for line in fileinput.input(): 10 + a, b, c = [int(x) for x in line.split(',')] 11 + BOXES.append((a, b, c)) 12 + 13 + 14 + # Precompute pairwise distances and sort ascending. 15 + DISTS = [] 16 + for y, (a1, b1, c1) in enumerate(BOXES): 17 + for x, (a2, b2, c2) in enumerate(BOXES): 18 + if x >= y: 19 + continue 20 + dist = (abs(a2 - a1) ** 2) + (abs(b2 - b1) ** 2) + (abs(c2 - c1) ** 2) 21 + DISTS.append((dist, x, y)) 22 + DISTS.sort() 23 + 24 + # Solve problem. 25 + TARGET_MERGES = 1000 26 + uf = UnionFind(len(BOXES)) 27 + last_merge = None 28 + for it, (dist, x, y) in enumerate(DISTS): 29 + if not uf.in_same_set(x, y): 30 + uf.merge(x, y) 31 + last_merge = (BOXES[x], BOXES[y]) 32 + 33 + if it == TARGET_MERGES - 1: 34 + circuits = [uf.find(i) for i in range(len(BOXES))] 35 + sizes = [size for circuit, size in Counter(circuits).most_common()] 36 + print("Part 1:", sizes[0] * sizes[1] * sizes[2]) 37 + 38 + print("Part 2:", last_merge[0][0] * last_merge[1][0])