-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday21.py
277 lines (220 loc) · 7.99 KB
/
day21.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import aoc
from collections import deque
"""
--- Day 21: Step Counter ---
You manage to catch the airship right as it's dropping someone else off on their all-expenses-paid trip to Desert Island!
It even helpfully drops you off near the gardener and his massive farm.
"You got the sand flowing again!
Great work!
Now we just need to wait until we have enough sand to filter the water for Snow Island and we'll have snow again in no time."
While you wait, one of the Elves that works with the gardener heard how good you are at solving problems and would like your help.
He needs to get his steps in for the day, and so he'd like to know which garden plots he can reach with exactly his remaining 64 steps.
He gives you an up-to-date map (your puzzle input) of his starting position (S), garden plots (.), and rocks (#).
For example:
...........
.....###.#.
.###.##..#.
..#.#...#..
....#.#....
.##..S####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........
The Elf starts at the starting position (S) which also counts as a garden plot.
Then, he can take one step north, south, east, or west, but only onto tiles that are garden plots.
This would allow him to reach any of the tiles marked O:
...........
.....###.#.
.###.##..#.
..#.#...#..
....#O#....
.##.OS####.
.##..#...#.
.......##..
.##.#.####.
.##..##.##.
...........
Then, he takes a second step.
Since at this point he could be at either tile marked O, his second step would allow him to reach any garden plot that is one step north, south, east, or west of any tile that he could have reached after the first step:
...........
.....###.#.
.###.##..#.
..#.#O..#..
....#.#....
.##O.O####.
.##.O#...#.
.......##..
.##.#.####.
.##..##.##.
...........
After two steps, he could be at any of the tiles marked O above, including the starting position (either by going north-then-south or by going west-then-east).
A single third step leads to even more possibilities:
...........
.....###.#.
.###.##..#.
..#.#.O.#..
...O#O#....
.##.OS####.
.##O.#...#.
....O..##..
.##.#.####.
.##..##.##.
...........
He will continue like this until his steps for the day have been exhausted.
After a total of 6 steps, he could reach any of the garden plots marked O:
...........
.....###.#.
.###.##.O#.
.O#O#O.O#..
O.O.#.#.O..
.##O.O####.
.##.O#O..#.
.O.O.O.##..
.##.#.####.
.##O.##.##.
...........
In this example, if the Elf's goal was to get exactly 6 more steps today, he could use them to reach any of 16 garden plots.
However, the Elf actually needs to get 64 steps today, and the map he's handed you is much larger than the example map.
Starting from the garden plot marked S on your map, how many garden plots could the Elf reach in exactly 64 steps?
--- Part Two ---
The Elf seems confused by your answer until he realizes his mistake:
he was reading from a list of his favorite numbers that are both perfect squares and perfect cubes, not his step counter.
The actual number of steps he needs to get today is exactly 26501365.
He also points out that the garden plots and rocks are set up so that the map repeats infinitely in every direction.
So, if you were to look one additional map-width or map-height out from the edge of the example map above, you would find that it keeps repeating:
.................................
.....###.#......###.#......###.#.
.###.##..#..###.##..#..###.##..#.
..#.#...#....#.#...#....#.#...#..
....#.#........#.#........#.#....
.##...####..##...####..##...####.
.##..#...#..##..#...#..##..#...#.
.......##.........##.........##..
.##.#.####..##.#.####..##.#.####.
.##..##.##..##..##.##..##..##.##.
.................................
.................................
.....###.#......###.#......###.#.
.###.##..#..###.##..#..###.##..#.
..#.#...#....#.#...#....#.#...#..
....#.#........#.#........#.#....
.##...####..##..S####..##...####.
.##..#...#..##..#...#..##..#...#.
.......##.........##.........##..
.##.#.####..##.#.####..##.#.####.
.##..##.##..##..##.##..##..##.##.
.................................
.................................
.....###.#......###.#......###.#.
.###.##..#..###.##..#..###.##..#.
..#.#...#....#.#...#....#.#...#..
....#.#........#.#........#.#....
.##...####..##...####..##...####.
.##..#...#..##..#...#..##..#...#.
.......##.........##.........##..
.##.#.####..##.#.####..##.#.####.
.##..##.##..##..##.##..##..##.##.
.................................
This is just a tiny three-map-by-three-map slice of the inexplicably-infinite farm layout;
garden plots and rocks repeat as far as you can see.
The Elf still starts on the one middle tile marked S, though - every other repeated S is replaced with a normal garden plot (.).
Here are the number of reachable garden plots in this new infinite version of the example map for different numbers of steps:
In exactly 6 steps, he can still reach 16 garden plots.
In exactly 10 steps, he can reach any of 50 garden plots.
In exactly 50 steps, he can reach 1594 garden plots.
In exactly 100 steps, he can reach 6536 garden plots.
In exactly 500 steps, he can reach 167004 garden plots.
In exactly 1000 steps, he can reach 668697 garden plots.
In exactly 5000 steps, he can reach 16733044 garden plots.
However, the step count the Elf needs is much larger!
Starting from the garden plot marked S on your infinite map, how many garden plots could the Elf reach in exactly 26501365 steps?
"""
'''
26501365 is target
Its factors are: 1, 5, 11, 55, 481843, 2409215, 5300273, 26501365
The grid is 131 x 131
Start is 65, 65
(26501365 - 65) % 131 = 0
Grid_Size = 131
Compute number of locations in infinite grid at step counts of:
65, 65 + 131, 65 + 131 * 2
The number of locations in steps to the mid-points of grids in the infinite grid
is a quadratic function of the grid count.
loc(grid_size) = a * grid_size^2 + b * grid_size + c
a + b + c = loc(1) => a + b = loc(1) - loc(0)
4a + 2b + c = loc(2) => 4a + 2b = loc(2) - loc(0)
2a = loc(2) - loc(0) - 2 * (loc(1) - loc(0)) =>
a = (loc(2) - 2 * loc(1) + loc(0))/2
b = loc(1) - loc(0) - a
c = loc(0)
Get loc(65, 65+131, 65 * 131*2)
'''
def parse(lines):
grid = []
for l in lines:
l = l.strip()
if len(l) == 0:
continue
if 'S' in l:
x = l.index('S')
y = len(grid)
startPos = (x, y)
grid.append(list(l))
grid[startPos[1]][startPos[0]] = '.'
return grid, startPos
def countLocations(lines, maxSteps):
grid, startPos = parse(lines)
h = len(grid)
w = len(grid[0])
locations = deque()
locations.append(startPos)
visited = {startPos: 0}
results = []
step = 0
maxStep = max(maxSteps)
while step < maxStep:
step += 1
newLocations = deque()
while locations:
(x, y) = locations.popleft()
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
nx = x + dx
ny = y + dy
gx = nx % w
gy = ny % h
if grid[gy][gx] == '.' and (nx, ny) not in visited:
newLocations.append((nx, ny))
visited[(nx, ny)] = step
locations = newLocations
if step in maxSteps:
results.append(len([d for d in visited.values() if d % 2 == step % 2]))
return results
def solvePart1(lines):
return countLocations(lines, [64])[0]
def solvePart2(lines):
numSamples = 4
maxSteps = []
for i in range(numSamples):
maxSteps.append(65 + 131 * i)
locs = countLocations(lines, maxSteps)
#a = (loc(2) - 2 * loc(1) + loc(0))/2
#b = loc(1) - loc(0) - a
#c = loc(0)
a = (locs[2] - 2 * locs[1] + locs[0]) // 2
b = locs[1] - locs[0] - a
c = locs[0]
# Check the quadratic equation fits
for i in range(numSamples):
if locs[i] != a * i * i + b * i + c:
print(f'Error in calculation at {i} {locs[i]} != {a * i * i + b * i + c}')
return 0
N = 26501365
grids = (N - 65)//131
result = a * grids * grids + b * grids + c
return result
def main():
aoc.run_day(21, solvePart1, 3562, solvePart2, 592723929260582)
if __name__ == '__main__':
main()