-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15b.py
40 lines (28 loc) · 910 Bytes
/
15b.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import numpy as np
import networkx as nx
def pipo(x):
return [ int(d) for d in x ]
smallcave = np.array([ pipo(x) for x in open('input.txt').read().splitlines() ])
maxy, maxx = smallcave.shape
cave = np.zeros((maxy*5, maxx*5), dtype=np.uint8)
for cy in range(5):
for cx in range(5):
c = (smallcave + cy + cx - 1) % 9 + 1
cave[maxy*cy:maxy*(cy+1), maxx*cx:maxx*(cx+1)] = c
maxy, maxx = cave.shape
g = nx.DiGraph()
g.add_nodes_from(range(cave.size))
def node(x,y):
return y*maxx+x
for y in range(maxy):
for x in range(maxx):
n = node(x,y)
if x < maxx-1:
n2 = node(x+1,y)
g.add_edge(n, n2, weight=cave[x+1,y] )
g.add_edge(n2, n, weight=cave[x,y] )
if y < maxy-1:
n2 = node(x,y+1)
g.add_edge(n, n2, weight=cave[x,y+1] )
g.add_edge(n2, n, weight=cave[x,y] )
print(nx.shortest_path_length(g, source=0, target=cave.size-1, weight='weight'))