-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_12.py
96 lines (73 loc) · 2.25 KB
/
day_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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import aoc_helper
from aoc_helper import (
Grid,
PrioQueue,
SparseGrid,
decode_text,
extract_ints,
extract_iranges,
extract_ranges,
extract_uints,
frange,
irange,
iter,
list,
map,
range,
tail_call,
)
import sys
from collections import deque
from typing import Tuple
raw = aoc_helper.fetch(12, 2022)
# raw = """Sabqponm
# abcryxxl
# accszExk
# acctuvwj
# abdefghi"""
def parse_raw(raw) -> Tuple[Tuple[int,int], Tuple[int,int], Grid[int]]:
g = Grid([]).from_string(raw, ord)
for i in range(len(g.data)):
for j in range(len(g.data[0])):
if g[i][j] == ord('S'): s = (j,i)
if g[i][j] == ord('E'): e = (j,i)
# print(s,e)
g[s[1]][s[0]] = ord('a')
g[e[1]][e[0]] = ord('z')
return (s,e,g)
data = parse_raw(raw)
def bfs(start, end, grid:Grid):
visited = set()
inqueu = set([start])
qu = deque([(start,0)])
while qu:
curr, dist = qu.pop()
visited.add(curr)
inqueu.remove(curr)
if curr == end: return dist
for point, height in grid.orthogonal_neighbours(*curr):
if point in visited or point in inqueu: continue
if height <= grid[curr[1]][curr[0]]+1:
qu.appendleft((point, dist + 1))
inqueu.add(point)
def part_one(data:Tuple[Tuple[int,int], Tuple[int,int], Grid[int]]):
return bfs(*data)
aoc_helper.lazy_test(day=12, year=2022, parse=parse_raw, solution=part_one)
def part_two(data):
_,end,grid = data
visited = set()
inqueu = set([end])
qu = deque([(end,0)])
while qu:
curr, dist = qu.pop()
visited.add(curr)
inqueu.remove(curr)
if grid[curr[1]][curr[0]] == ord('a'): return dist
for point, height in grid.orthogonal_neighbours(*curr):
if point in visited or point in inqueu: continue
if height >= grid[curr[1]][curr[0]]-1:
qu.appendleft((point, dist + 1))
inqueu.add(point)
aoc_helper.lazy_test(day=12, year=2022, parse=parse_raw, solution=part_two)
aoc_helper.lazy_submit(day=12, year=2022, solution=part_one, data=data)
aoc_helper.lazy_submit(day=12, year=2022, solution=part_two, data=data)