Skip to content

Commit 10f5399

Browse files
committed
Add solution to 2024-12-12
1 parent 680bb87 commit 10f5399

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

2024/day12/solutions.py

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

0 commit comments

Comments
 (0)