···11+import fileinput
22+from collections import deque
33+from utils import Point
44+55+# Parse input.
66+board = {}
77+for y, line in enumerate(fileinput.input()):
88+ for x, c in enumerate(line.strip()):
99+ board[Point(x, y)] = c
1010+ if c == 'S':
1111+ start = Point(x, y)
1212+ board[start] = 'a'
1313+ elif c == 'E':
1414+ end = Point(x, y)
1515+ board[end] = 'z'
1616+1717+1818+def bfs(start):
1919+ horizon = deque([(start, 0)])
2020+ seen = set()
2121+ while horizon:
2222+ p, depth = horizon.popleft()
2323+ if p in seen:
2424+ continue
2525+ elif p == end:
2626+ return depth
2727+2828+ seen.add(p)
2929+3030+ for n in p.neighbours(): # returns the 4 adjacent points
3131+ if n not in board:
3232+ continue
3333+ if (ord(board[n]) - 1 <= ord(board[p])):
3434+ horizon.append((n, depth + 1))
3535+3636+ return 1e9
3737+3838+3939+print("Part 1:", bfs(start))
4040+print("Part 2:", min(bfs(s) for s in board if board[s] == 'a'))