We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 680bb87 commit 10f5399Copy full SHA for 10f5399
2024/day12/solutions.py
@@ -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