-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.py
37 lines (27 loc) · 1 KB
/
12.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 lib import *
input = read_input(2022, 12)
grid = []
start = None
end = None
for i, line in enumerate(input.splitlines()):
grid.append([ord(c) - 97 if c not in "SE" else {"S": 0, "E": 25}[c] for c in line])
if "S" in line:
start = line.index("S"), i
if "E" in line:
end = line.index("E"), i
assert start and end
def bfs(start: tuple[int, int], grid, target, step):
queue: list[tuple[int, int, int]] = [(0, *start)]
visited = set()
while queue:
d, x, y = queue.pop(0)
if target(x, y):
return d
if (x, y) in visited:
continue
visited.add((x, y))
for p, q in get_neighbors(x, y, len(grid[0]), len(grid)):
if step(x, y, p, q) and (p, q) not in visited:
queue.append((d + 1, p, q))
print(bfs(start, grid, lambda x, y: (x, y) == end, lambda x, y, p, q: grid[q][p] - grid[y][x] <= 1))
print(bfs(end, grid, lambda x, y: grid[y][x] == 0, lambda x, y, p, q: grid[y][x] - grid[q][p] <= 1))