-
Notifications
You must be signed in to change notification settings - Fork 0
/
D16.py
249 lines (215 loc) · 8.44 KB
/
D16.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
'''
# PART 1
## Input
.|...\....
|.-.\.....
.....|-...
........|.
..........
.........\
..../.\\..
.-.-/..|..
.|....-|.\
..//.|....
## Description
the grid above shows mirrors. The light enters at the upper left corner from the left,
and continues in the same direction unless it hits a line. \ an / turn the beam 90 degrees,
while - and | split the beam at 90 degree angles if they are hit the long way. How
many squares of the grid will the beam of light energize?
## approach
This is very similar to a previous day, where we follow a path based on the characters
in the grid. The only additional element is a queue to keep track of diverging paths
when the beam splits.
'''
with open("D16.txt","r") as file:
input = file.read().splitlines()
TOP = 'top'
BOTTOM = 'bottom'
LEFT = 'left'
RIGHT = 'right'
for r in range(len(input)):
print (f'[{r}] {input[r]}')
row_count = len(input)
col_count = len(input[0])
# helper functions for moving around the grid
def go_left(r, c, entry):
if c != 0:
return (r, c-1, RIGHT)
return (r,c,entry)
def go_right(r, c,entry):
if c != col_count-1:
return (r, c+1, LEFT)
return (r,c,entry)
def go_up(r,c,entry):
if r != 0:
return (r-1, c, BOTTOM)
return (r,c,entry)
def go_down(r,c,entry):
if r != row_count-1:
return (r+1, c, TOP)
return (r,c,entry)
def part1():
# format will be (r,c,entry)
visited = set()
# store branches to come back to later
queue = []
# start in the upper left corner,
# coming in from the left
r=0
c=0
entry=LEFT
#follow the path
while (r,c,entry) not in visited or len(queue) > 0:
if (r,c,entry) in visited:
(r,c,entry) = queue.pop()
if (r,c,entry) in visited:
continue
visited.add((r,c,entry))
match input[r][c]:
case '|':
if entry == LEFT or RIGHT:
previous=(r,c,entry)
(r,c,entry) = go_up(r,c,entry)
queue.append(go_down(*previous))
elif entry == BOTTOM:
(r,c,entry) = go_up(r,c,entry)
elif entry == TOP:
(r,c,entry) = go_down(r,c,entry)
case '-':
if entry == TOP or BOTTOM:
previous=(r,c,entry)
(r,c,entry) = go_left(r,c,entry)
queue.append(go_right(*previous))
elif entry == LEFT:
(r,c,entry) = go_right(r,c,entry)
elif entry == RIGHT:
(r,c,entry) = go_left(r,c,entry)
case '\\':
if entry == LEFT:
(r,c,entry) = go_down(r,c,entry)
elif entry == BOTTOM:
(r,c,entry) = go_left(r,c,entry)
elif entry == TOP:
(r,c,entry) = go_right(r,c,entry)
elif entry == RIGHT:
(r,c,entry) = go_up(r,c,entry)
case '/':
if entry == LEFT:
(r,c,entry) = go_up(r,c,entry)
elif entry == BOTTOM:
(r,c,entry) = go_right(r,c,entry)
elif entry == TOP:
(r,c,entry) = go_left(r,c,entry)
elif entry == RIGHT:
(r,c,entry) = go_down(r,c,entry)
case '.':
if entry == LEFT:
(r,c,entry) = go_right(r,c,entry)
elif entry == BOTTOM:
(r,c,entry) = go_up(r,c,entry)
elif entry == TOP:
(r,c,entry) = go_down(r,c,entry)
elif entry == RIGHT:
(r,c,entry) = go_left(r,c,entry)
visited_no_direction = set()
for (r,c,_) in visited:
visited_no_direction.add((r,c))
print(f'part 1: {len(visited_no_direction)}')
'''
# Part 2
Now we want to try every possible entry to the grid, and find the one that will
light up the most squares.
## Approach
So I built all this stuff thinking that this was going to need to be optimized,
and int the process I finally learned a lot about how recursion (doesn't) work
in Python, and then eventually I just ran the code on the test input without
any of the optimization, and it ended in a very reasonable time.
So my approach ended up being to apply the code from part 1 to each option and take
the one with the longest path. But there's a bunch of extra code here that could
be optimization someday.
'''
class MapTraversal:
map = list[str]
lights_from_here: dict
def __init__(self, map):
# this doesn't do anything but in theory it could be a lookup
# table of {(r,c,entrypoint): number of tiles that light up from that combo}
self.lights_from_here = {}
self.map = map
#returns the numer of tiles that will light up starting from this point.
def traverse(self,r,c,entry):
if (r,c,entry) not in self.lights_from_here:
self.lights_from_here[(r,c,entry)] = self.really_traverse(r,c,entry)
return self.lights_from_here[(r,c,entry)]
def really_traverse(self,r,c,entry):
# format will be (r,c,entry)
visited = set()
# store branches to come back to later
queue = []
#follow the path
while (r,c,entry) not in visited or len(queue) > 0:
if (r,c,entry) in visited:
(r,c,entry) = queue.pop()
if (r,c,entry) in visited:
continue
visited.add((r,c,entry))
match input[r][c]:
case '|':
if entry == LEFT or RIGHT:
previous=(r,c,entry)
(r,c,entry) = go_up(r,c,entry)
queue.append(go_down(*previous))
elif entry == BOTTOM:
(r,c,entry) = go_up(r,c,entry)
elif entry == TOP:
(r,c,entry) = go_down(r,c,entry)
case '-':
if entry == TOP or BOTTOM:
previous=(r,c,entry)
(r,c,entry) = go_left(r,c,entry)
queue.append(go_right(*previous))
elif entry == LEFT:
(r,c,entry) = go_right(r,c,entry)
elif entry == RIGHT:
(r,c,entry) = go_left(r,c,entry)
case '\\':
if entry == LEFT:
(r,c,entry) = go_down(r,c,entry)
elif entry == BOTTOM:
(r,c,entry) = go_left(r,c,entry)
elif entry == TOP:
(r,c,entry) = go_right(r,c,entry)
elif entry == RIGHT:
(r,c,entry) = go_up(r,c,entry)
case '/':
if entry == LEFT:
(r,c,entry) = go_up(r,c,entry)
elif entry == BOTTOM:
(r,c,entry) = go_right(r,c,entry)
elif entry == TOP:
(r,c,entry) = go_left(r,c,entry)
elif entry == RIGHT:
(r,c,entry) = go_down(r,c,entry)
case '.':
if entry == LEFT:
(r,c,entry) = go_right(r,c,entry)
elif entry == BOTTOM:
(r,c,entry) = go_up(r,c,entry)
elif entry == TOP:
(r,c,entry) = go_down(r,c,entry)
elif entry == RIGHT:
(r,c,entry) = go_left(r,c,entry)
visited_no_direction = set()
for (r,c,_) in visited:
visited_no_direction.add((r,c))
# print(visited)
return len(visited_no_direction)
def part2():
map = MapTraversal(input)
top = [map.traverse(0,n,TOP) for n in range(len(input[0]))]
bottom = [map.traverse(len(input)-1,n,BOTTOM) for n in range(len(input[0]))]
left = [map.traverse(n,0,LEFT) for n in range(len(input))]
right = [map.traverse(n,len(input[0])-1,RIGHT) for n in range(len(input))]
print(f'part 2: {max([*top, *bottom, *left, *right])}')
part1()
part2()