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 7455dbcCopy full SHA for 7455dbc
2024/day12/solutions.py
@@ -0,0 +1,30 @@
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
21
+ wall = {(z, dz * 1j) for dz in fourdir for z in comp if z + dz not in comp}
22
+ res1 += len(comp) * len(wall)
23
24
+ res2 += len(comp) * sum((z + dz, dz) not in wall for (z, dz) in wall)
25
26
+# Part 1
27
+print(res1)
28
29
+# Part 2
30
+print(res2)
0 commit comments