Skip to content

Commit 818dd39

Browse files
committed
Add solution to 2024-12-12
1 parent 680bb87 commit 818dd39

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

2024/day12/solutions.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import networkx as nx
2+
from scipy.cluster.hierarchy import DisjointSet
3+
4+
5+
with open("input") as f:
6+
board = {
7+
i + 1j * j: x
8+
for i, l in enumerate(f.read().strip().split("\n"))
9+
for j, x in enumerate(l)
10+
}
11+
12+
fourdir = {1, -1, 1j, -1j}
13+
G = nx.Graph(
14+
(z, z + dz) for z in board for dz in fourdir if board[z] == board.get(z + dz)
15+
)
16+
G.add_nodes_from(board)
17+
18+
res1 = res2 = 0
19+
for comp in nx.connected_components(G):
20+
wall = {(z, dz * 1j) for dz in fourdir for z in comp if z + dz not in comp}
21+
22+
res1 += len(comp) * len(wall)
23+
res2 += len(comp) * sum((z + dz, dz) not in wall for (z, dz) in wall)
24+
25+
# Part 1
26+
print(res1)
27+
28+
# Part 2
29+
print(res2)

0 commit comments

Comments
 (0)