|
1
|
|
|
from __future__ import annotations |
|
2
|
|
|
|
|
3
|
|
|
from collections import namedtuple |
|
4
|
|
|
import pathlib |
|
5
|
|
|
|
|
6
|
|
|
from typing import Final, List, Optional |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
Point = namedtuple('Point', ('x', 'y')) |
|
10
|
|
|
|
|
11
|
|
|
ELEVATIONS: Final[str] = "_abcdefghijklmnopqrstuvwxyz_" |
|
12
|
|
|
|
|
13
|
|
|
def find_on(world: List[str], symbol: str, rep: str) -> Optional[Point]: |
|
14
|
|
|
for y, line in enumerate(world): |
|
15
|
|
|
if symbol not in line: |
|
16
|
|
|
continue |
|
17
|
|
|
|
|
18
|
|
|
x: int = line.index(symbol) |
|
19
|
|
|
world[y] = line.replace(symbol, rep, 1) |
|
20
|
|
|
return Point(x, y) |
|
21
|
|
|
return None |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
def get_possible_neighbors(world: List[str], me: Point) -> List[Point]: |
|
25
|
|
|
elevation: str = world[me.y][me.x] |
|
26
|
|
|
|
|
27
|
|
|
_e_id: int = ELEVATIONS.index(elevation) |
|
28
|
|
|
accept: str = ELEVATIONS[_e_id - 1] + ELEVATIONS[_e_id + 1] |
|
29
|
|
|
|
|
30
|
|
|
# Todo: Iter 4 neighbor -> check bound -> check elevation -> yield |
|
31
|
|
|
return [] |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
def shortest_path( |
|
36
|
|
|
world: List[str], |
|
37
|
|
|
me: Point, |
|
38
|
|
|
target: Point, |
|
39
|
|
|
path: Optional[List[Point]] = None |
|
40
|
|
|
) -> List[Point]: |
|
41
|
|
|
if not path: |
|
42
|
|
|
path = [me] |
|
43
|
|
|
|
|
44
|
|
|
if not world or (me == target): |
|
45
|
|
|
return path |
|
46
|
|
|
|
|
47
|
|
|
for neighbor in get_possible_neighbors(world, me): |
|
48
|
|
|
return shortest_path(world, me, target, path + [neighbor]) |
|
49
|
|
|
|
|
50
|
|
|
return path |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
def main() -> None: |
|
54
|
|
|
content: str = pathlib.Path('./input.txt').read_text() |
|
55
|
|
|
world_map: List[str] = content.splitlines() |
|
56
|
|
|
|
|
57
|
|
|
me: Optional[Point] = find_on(world_map, 'S', rep='a') |
|
58
|
|
|
if me is None: |
|
59
|
|
|
return None |
|
60
|
|
|
target: Optional[Point] = find_on(world_map, 'E', rep='z') |
|
61
|
|
|
if target is None: |
|
62
|
|
|
return None |
|
63
|
|
|
|
|
64
|
|
|
print(me, '->', target) |
|
65
|
|
|
print(shortest_path(world_map, me, target)) |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
if __name__ == '__main__': |
|
69
|
|
|
main() |
|
70
|
|
|
|