Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution to 2024-12-12
Browse files Browse the repository at this point in the history
fuglede committed Dec 12, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 680bb87 commit 818dd39
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions 2024/day12/solutions.py
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)

0 comments on commit 818dd39

Please sign in to comment.