Skip to content

Commit

Permalink
Add solution to 2024-12-16
Browse files Browse the repository at this point in the history
  • Loading branch information
fuglede committed Dec 16, 2024
1 parent 8757046 commit 2db1026
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions 2024/day16/solutions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import networkx as nx

with open("input") as f:
ls = f.read().strip().split("\n")

fourdir = (1, -1, 1j, -1j)

G = nx.DiGraph()

for i, l in enumerate(ls):
for j, x in enumerate(l):
if x == "#":
continue
z = i + 1j * j
if x == "S":
start = (z, 1j)
if x == "E":
end = z
for dz in fourdir:
G.add_node((z, dz))

for z, dz in G.nodes:
if (z + dz, dz) in G.nodes:
G.add_edge((z, dz), (z + dz, dz), weight=1)
for rot in -1j, 1j:
G.add_edge((z, dz), (z, dz * rot), weight=1000)

for dz in fourdir:
G.add_edge((end, dz), "end", weight=0)

# Part 1
print(nx.shortest_path_length(G, start, "end", weight="weight"))

# Part2
print(
len(
{
z
for path in nx.all_shortest_paths(G, start, "end", weight="weight")
for z, _ in path[:-1]
}
)
)

0 comments on commit 2db1026

Please sign in to comment.