-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
37 lines (31 loc) · 963 Bytes
/
main.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
from networkx import grid_2d_graph
from networkx import shortest_path
with open("input.txt") as f:
grid = [list(x.strip()) for x in f]
source = None
all_sources = []
target = None
for row_n, row in enumerate(grid):
for col_n, c in enumerate(row):
if c == "S":
source = (row_n, col_n)
grid[row_n][col_n] = "a"
if c == "E":
target = (row_n, col_n)
grid[row_n][col_n] = "z"
if grid[row_n][col_n] == "a":
all_sources.append((row_n, col_n))
g = grid_2d_graph(len(grid), len(grid[0]))
grid = [[ord(c)-ord("a") for c in row] for row in grid]
def weight_func(a, b, edge_dict):
if grid[a[0]][a[1]] < grid[b[0]][b[1]] - 1:
return None
return 1
print(len(shortest_path(g, source, target, weight_func))-1)
paths = []
for s in all_sources:
try:
paths.append(len(shortest_path(g, s, target, weight_func))-1)
except:
pass
print(min(paths))