-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday8.py
More file actions
81 lines (58 loc) · 1.91 KB
/
day8.py
File metadata and controls
81 lines (58 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Advent of Code 2025, Day 8
# (c) blu3r4y
import networkx as nx
import numpy as np
from aocd.models import Puzzle
from funcy import print_calls, print_durations
@print_calls
@print_durations(unit="ms")
def part1(points, max_connections=1000):
G, _ = solve(points, max_connections=max_connections)
# sizes of isolated subgraphs
components = list(nx.connected_components(G))
sizes = sorted([len(c) for c in components], reverse=True)
# product of sizes of the three largest components
return int(sizes[0] * sizes[1] * sizes[2])
@print_calls
@print_durations(unit="ms")
def part2(points):
_, lastedge = solve(points)
# multiply x-coordinates of last edge
return int(lastedge[0][0] * lastedge[1][0])
@print_durations(unit="ms")
def solve(points, max_connections=None):
num_points = len(points)
pairs = []
for i in range(num_points):
for j in range(i + 1, num_points):
dist = np.linalg.norm(points[i] - points[j])
pairs.append((dist, i, j))
# sort by distance
pairs.sort(key=lambda x: x[0])
# build graph
G = nx.Graph()
G.add_nodes_from(range(num_points))
lastedge = None
# keep connecting closest pairs
for _, u, v in pairs[:max_connections]:
if not nx.has_path(G, u, v):
G.add_edge(u, v)
# stop if all points are connected
if nx.number_connected_components(G) == 1:
lastedge = (points[u], points[v])
break
return G, lastedge
def load(data):
points = []
for line in data.splitlines():
nums = list(map(int, line.split(",")))
points.append(nums)
return np.array(points)
if __name__ == "__main__":
puzzle = Puzzle(year=2025, day=8)
ans1 = part1(load(puzzle.input_data))
assert ans1 == 330786
puzzle.answer_a = ans1
ans2 = part2(load(puzzle.input_data))
assert ans2 == 3276581616
puzzle.answer_b = ans2