-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Add solution to 2024-12-12
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import networkx as nx | ||
from scipy.cluster.hierarchy import DisjointSet | ||
|
||
|
||
with open("input") as f: | ||
board = { | ||
i + 1j * j: x | ||
for i, l in enumerate(f.read().strip().split("\n")) | ||
for j, x in enumerate(l) | ||
} | ||
|
||
fourdir = {1, -1, 1j, -1j} | ||
G = nx.Graph( | ||
(z, z + dz) for z in board for dz in fourdir if board[z] == board.get(z + dz) | ||
) | ||
G.add_nodes_from(board) | ||
|
||
res1 = res2 = 0 | ||
for comp in nx.connected_components(G): | ||
wall = {(z, dz * 1j) for dz in fourdir for z in comp if z + dz not in comp} | ||
|
||
res1 += len(comp) * len(wall) | ||
res2 += len(comp) * sum((z + dz, dz) not in wall for (z, dz) in wall) | ||
|
||
# Part 1 | ||
print(res1) | ||
|
||
# Part 2 | ||
print(res2) |