-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday20.py
More file actions
161 lines (140 loc) · 4.77 KB
/
day20.py
File metadata and controls
161 lines (140 loc) · 4.77 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from collections import defaultdict
from itertools import islice
from re import findall
from aocd.models import Puzzle
from coord import Coord
from util import inspect
example_1 = [
" A ",
" A ",
" #######.######### ",
" #######.........# ",
" #######.#######.# ",
" #######.#######.# ",
" #######.#######.# ",
" ##### B ###.# ",
"BC...## C ###.# ",
" ##.## ###.# ",
" ##...DE F ###.# ",
" ##### G ###.# ",
" #########.#####.# ",
"DE..#######...###.# ",
" #.#########.###.# ",
"FG..#########.....# ",
" ###########.##### ",
" Z ",
" Z ",
]
example_2 = [
" A ",
" A ",
" #################.############# ",
" #.#...#...................#.#.# ",
" #.#.#.###.###.###.#########.#.# ",
" #.#.#.......#...#.....#.#.#...# ",
" #.#########.###.#####.#.#.###.# ",
" #.............#.#.....#.......# ",
" ###.###########.###.#####.#.#.# ",
" #.....# A C #.#.#.# ",
" ####### S P #####.# ",
" #.#...# #......VT",
" #.#.#.# #.##### ",
" #...#.# YN....#.# ",
" #.###.# #####.# ",
"DI....#.# #.....# ",
" #####.# #.###.# ",
"ZZ......# QG....#..AS",
" ###.### ####### ",
"JO..#.#.# #.....# ",
" #.#.#.# ###.#.# ",
" #...#..DI BU....#..LF",
" #####.# #.##### ",
"YN......# VT..#....QG",
" #.###.# #.###.# ",
" #.#...# #.....# ",
" ###.### J L J #.#.### ",
" #.....# O F P #.#...# ",
" #.###.#####.#.#####.#####.###.# ",
" #...#.#.#...#.....#.....#.#...# ",
" #.#####.###.###.#.#.#########.# ",
" #...#.#.....#...#.#.#.#.....#.# ",
" #.###.#####.###.###.#.#.####### ",
" #.#.........#...#.............# ",
" #########.###.###.############# ",
" B J C ",
" U P P ",
]
def process_line(line, coord_builder, portals, unmatched):
if found := findall(r'([A-Z]{2})', line):
for portal in found:
pos = line.find(portal)
if pos > 0 and line[pos - 1] == '.':
var = pos - 1
elif pos < (len(line) - 2) and line[pos + 2] == '.':
var = pos + 2
else:
raise Exception("can't find portal space")
coordinates = coord_builder(var)
if portal in unmatched:
portals[coordinates] = unmatched[portal]
portals[unmatched[portal]] = coordinates
else:
unmatched[portal] = coordinates
def reconstruct_path(camefrom, pos):
path = []
while pos in camefrom:
pos = camefrom[pos]
path.insert(0, pos)
return path
def pathfind(donut, portals, start, finish):
openset = {start}
camefrom = {}
gscore = defaultdict(lambda: float('inf'))
gscore[start] = 0
# shortest = float('inf')
while openset:
current = openset.pop()
if current == finish:
length = len(reconstruct_path(camefrom, current))
return length
# if length < shortest:
# shortest = length
options = [nbor for nbor in current.neighbours if donut[nbor.y][nbor.x] == '.']
if current in portals:
options.append(portals[current])
for option in options:
new_gscore = gscore[current] + 1
if new_gscore < gscore[option]:
camefrom[option] = current
gscore[option] = new_gscore
openset.add(option)
# return shortest
def part1(donut):
"""
>>> part1(example_1)
23
>>> part1(example_2)
58
"""
portals = {}
unmatched = {}
for y, row in enumerate(donut):
process_line(row, lambda x: Coord(x, y), portals, unmatched)
for x in range(len(donut[0])):
column = ''.join([row[x] for row in donut])
process_line(column, lambda y: Coord(x, y), portals, unmatched)
begin = unmatched['AA']
end = unmatched['ZZ']
return pathfind(donut, portals, begin, end)
# def part2(donut):
# """
# >>> part2()
#
# """
if __name__ == "__main__":
import doctest
doctest.testmod()
puzzle = Puzzle(year=2019, day=20)
donut = puzzle.input_data.split('\n')
puzzle.answer_a = inspect(part1(donut), prefix='Part 1: ')
# puzzle.answer_b = inspect(part2(donut), prefix='Part 2: ')