Skip to content

Commit 2db1026

Browse files
committed
Add solution to 2024-12-16
1 parent 8757046 commit 2db1026

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

2024/day16/solutions.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import networkx as nx
2+
3+
with open("input") as f:
4+
ls = f.read().strip().split("\n")
5+
6+
fourdir = (1, -1, 1j, -1j)
7+
8+
G = nx.DiGraph()
9+
10+
for i, l in enumerate(ls):
11+
for j, x in enumerate(l):
12+
if x == "#":
13+
continue
14+
z = i + 1j * j
15+
if x == "S":
16+
start = (z, 1j)
17+
if x == "E":
18+
end = z
19+
for dz in fourdir:
20+
G.add_node((z, dz))
21+
22+
for z, dz in G.nodes:
23+
if (z + dz, dz) in G.nodes:
24+
G.add_edge((z, dz), (z + dz, dz), weight=1)
25+
for rot in -1j, 1j:
26+
G.add_edge((z, dz), (z, dz * rot), weight=1000)
27+
28+
for dz in fourdir:
29+
G.add_edge((end, dz), "end", weight=0)
30+
31+
# Part 1
32+
print(nx.shortest_path_length(G, start, "end", weight="weight"))
33+
34+
# Part2
35+
print(
36+
len(
37+
{
38+
z
39+
for path in nx.all_shortest_paths(G, start, "end", weight="weight")
40+
for z, _ in path[:-1]
41+
}
42+
)
43+
)

0 commit comments

Comments
 (0)