Skip to content

Commit 6c23c27

Browse files
committed
2022-12
1 parent 572bce8 commit 6c23c27

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

2022/12.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
import networkx as nx
3+
4+
5+
def path(graph, source, target):
6+
try:
7+
return nx.shortest_path_length(graph, source, target)
8+
except:
9+
return -1
10+
11+
12+
# fmt: off
13+
t = {
14+
complex(r, c): ord(s)
15+
for r, line in enumerate(open(0))
16+
for c, s in enumerate(line.strip())
17+
}
18+
# fmt: on
19+
s = next(k for k, v in t.items() if v == ord("S"))
20+
e = next(k for k, v in t.items() if v == ord("E"))
21+
22+
t[s] = ord("a")
23+
t[e] = ord("z")
24+
25+
G = nx.DiGraph()
26+
27+
for k, v in t.items():
28+
for d in (-1, 1, -1j, 1j):
29+
if (n := k + d) in t:
30+
if t[n] - v <= 1:
31+
G.add_edge(k, n)
32+
33+
print(nx.shortest_path_length(G, s, e))
34+
# fmt: off
35+
print(min(
36+
l
37+
for k, v in t.items()
38+
if v == ord("a") and (l := path(G, k, e)) >= 0
39+
))

0 commit comments

Comments
 (0)