-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path22.py
357 lines (342 loc) · 10.5 KB
/
22.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# --- Day 22: Monkey Map ---
#
# The monkeys take you on a surprisingly easy trail through the
# jungle. They're even going in roughly the right direction according
# to your handheld device's Grove Positioning System.
#
# As you walk, the monkeys explain that the grove is protected by a
# force field. To pass through the force field, you have to enter a
# password; doing so involves tracing a specific path on a
# strangely-shaped board.
#
# At least, you're pretty sure that's what you have to do; the
# elephants aren't exactly fluent in monkey.
#
# The monkeys give you notes that they took when they last saw the
# password entered (your puzzle input).
#
# For example:
#
# ...#
# .#..
# #...
# ....
# ...#.......#
# ........#...
# ..#....#....
# ..........#.
# ...#....
# .....#..
# .#......
# ......#.
#
# 10R5L5R10L4R5L5
#
# The first half of the monkeys' notes is a map of the board. It is
# comprised of a set of open tiles (on which you can move, drawn .)
# and solid walls (tiles which you cannot enter, drawn #).
#
# The second half is a description of the path you must follow. It
# consists of alternating numbers and letters:
#
# - A number indicates the number of tiles to move in the direction
# you are facing. If you run into a wall, you stop moving forward
# and continue with the next instruction.
# - A letter indicates whether to turn 90 degrees clockwise (R) or
# counterclockwise (L). Turning happens in-place; it does not
# change your current tile.
#
# So, a path like 10R5 means "go forward 10 tiles, then turn clockwise
# 90 degrees, then go forward 5 tiles".
#
# You begin the path in the leftmost open tile of the top row of
# tiles. Initially, you are facing to the right (from the perspective
# of how the map is drawn).
#
# If a movement instruction would take you off of the map, you wrap
# around to the other side of the board. In other words, if your next
# tile is off of the board, you should instead look in the direction
# opposite of your current facing as far as you can until you find the
# opposite edge of the board, then reappear there.
#
# For example, if you are at A and facing to the right, the tile in
# front of you is marked B; if you are at C and facing down, the tile
# in front of you is marked D:
#
# ...#
# .#..
# #...
# ....
# ...#.D.....#
# ........#...
# B.#....#...A
# .....C....#.
# ...#....
# .....#..
# .#......
# ......#.
#
# It is possible for the next tile (after wrapping around) to be a
# wall; this still counts as there being a wall in front of you, and
# so movement stops before you actually wrap to the other side of the
# board.
#
# By drawing the last facing you had with an arrow on each tile you
# visit, the full path taken by the above example looks like this:
#
# >>v#
# .#v.
# #.v.
# ..v.
# ...#...v..v#
# >>>v...>#.>>
# ..#v...#....
# ...>>>>v..#.
# ...#....
# .....#..
# .#......
# ......#.
#
# To finish providing the password to this strange input device, you
# need to determine numbers for your final row, column, and facing as
# your final position appears from the perspective of the original
# map. Rows start from 1 at the top and count downward; columns start
# from 1 at the left and count rightward. (In the above example, row
# 1, column 1 refers to the empty space with no tile on it in the
# top-left corner.) Facing is 0 for right (>), 1 for down (v), 2 for
# left (<), and 3 for up (^). The final password is the sum of 1000
# times the row, 4 times the column, and the facing.
#
# In the above example, the final row is 6, the final column is 8, and
# the final facing is 0. So, the final password is
# 1000 * 6 + 4 * 8 + 0: 6032.
#
# Follow the path given in the monkeys' notes. What is the final
# password?
import re
def load():
part1, part2 = open("22.in").read().split("\n\n")
lines = part1.split("\n")
max_len = max(len(l) for l in lines)
map = ["{:{width}}".format(l, width=max_len) for l in lines]
path = [
int(inst) if inst.isdigit() else inst
for inst in re.split(
"(?<=[LR])(?=[0-9])|(?<=[0-9])(?=[LR])",
part2.strip()
)
]
return map, path
map, path = load()
H, W = len(map), len(map[0]) # map dimensions, including voids
WALL, OPEN, VOID = "#. "
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
R, D, L, U = range(4) # right, down, left, up; indices into the above
def next_pos(r, c, dir):
nr, nc = r, c
while True:
nr, nc = (nr+directions[dir][0])%H, (nc+directions[dir][1])%W
if map[nr][nc] == WALL:
return (r, c, dir)
elif map[nr][nc] == OPEN:
return (nr, nc, dir)
else:
pass # in void; keep walking
def solve():
r, c = 0, map[0].index(OPEN)
dir = R
for inst in path:
if type(inst) is int:
for _ in range(inst):
r, c, dir = next_pos(r, c, dir)
else:
if inst == "L":
dir = (dir-1)%4
else:
dir = (dir+1)%4
print(1000*(r+1) + 4*(c+1) + dir)
solve()
# --- Part Two ---
#
# As you reach the force field, you think you hear some Elves in the
# distance. Perhaps they've already arrived?
#
# You approach the strange input device, but it isn't quite what the
# monkeys drew in their notes. Instead, you are met with a large
# cube; each of its six faces is a square of 50x50 tiles.
#
# To be fair, the monkeys' map does have six 50x50 regions on it. If
# you were to carefully fold the map, you should be able to shape it
# into a cube!
#
# In the example above, the six (smaller, 4x4) faces of the cube are:
#
# 1111
# 1111
# 1111
# 1111
# 222233334444
# 222233334444
# 222233334444
# 222233334444
# 55556666
# 55556666
# 55556666
# 55556666
#
# You still start in the same position and with the same facing as
# before, but the wrapping rules are different. Now, if you would
# walk off the board, you instead proceed around the cube. From the
# perspective of the map, this can look a little strange. In the
# above example, if you are at A and move to the right, you would
# arrive at B facing down; if you are at C and move down, you would
# arrive at D facing up:
#
# ...#
# .#..
# #...
# ....
# ...#.......#
# ........#..A
# ..#....#....
# .D........#.
# ...#..B.
# .....#..
# .#......
# ..C...#.
#
# Walls still block your path, even if they are on a different face of
# the cube. If you are at E facing up, your movement is blocked by
# the wall marked by the arrow:
#
# ...#
# .#..
# -->#...
# ....
# ...#..E....#
# ........#...
# ..#....#....
# ..........#.
# ...#....
# .....#..
# .#......
# ......#.
#
# Using the same method of drawing the last facing you had with an
# arrow on each tile you visit, the full path taken by the above
# example now looks like this:
#
# >>v#
# .#v.
# #.v.
# ..v.
# ...#..^...v#
# .>>>>>^.#.>>
# .^#....#....
# .^........#.
# ...#..v.
# .....#v.
# .#v<<<<.
# ..v...#.
#
# The final password is still calculated from your final position and
# facing from the perspective of the map. In this example, the final
# row is 5, the final column is 7, and the final facing is 3, so the
# final password is 1000 * 5 + 4 * 7 + 3 = 5031.
#
# Fold the map into a cube, then follow the path given in the monkeys'
# notes. What is the final password?
#
# --------------------
#
# Fun puzzle! The folding of our cube was determined by visual
# inspection and is illustrated below by face numbers and labels of
# shared edges:
#
# 1 1
# 5 0 5
# 0.....0.....0.....0 ---> c
#
# 0 +--g--+--d--+
# . | | |
# . e 1 | 0 b
# . | | |
# 50 +-----+--a--+
# . | |
# . c 2 a
# . | |
# 100 +--c--+-----+
# . | | |
# . e 4 | 3 b
# . | | |
# 150 +-----+--f--+
# . | |
# . g 5 f
# . | |
# 200 +--d--+
# |
# |
# v
# r
#
# We simulate traveling on the cube by moving normally on the map, but
# detecting when we are no longer enclosed in a face. Using the
# current face and direction, we determine the new face and new
# direction and coordinate transform. The transforms were
# pre-calculated only by cutting out pieces of graph paper and seeing
# how they fit together!
#
# Note that cube faces are in effect butted like so:
#
# |.|
# |.|
# |B|
# -----+-+
# ....A|O
# -----+
#
# The implication is that if we are at position A and move outside a
# face to position O, the transform must factor in the butting to
# return B as the next position.
from common import pick
faces = [ # (rrange, crange)
(range( 0, 50), range(100, 150)), # 0
(range( 0, 50), range( 50, 100)), # 1
(range( 50, 100), range( 50, 100)), # 2
(range(100, 150), range( 50, 100)), # 3
(range(100, 150), range( 0, 50)), # 4
(range(150, 200), range( 0, 50)) # 5
]
def face_number(r, c):
# Return the number of the enclosing face of a position, or None.
try:
return pick(lambda i: r in faces[i][0] and c in faces[i][1], range(6))
except StopIteration:
return None
# (from face, direction) => (to face, direction, coordinate transform)
edges = {
(2, R): (0, U, lambda r, c: (49, r+50)), # a
(0, D): (2, L, lambda r, c: (c-50, 99)),
(3, R): (0, L, lambda r, c: (149-r, 149)), # b
(0, R): (3, L, lambda r, c: (149-r, 99)),
(2, L): (4, D, lambda r, c: (100, r-50)), # c
(4, U): (2, R, lambda r, c: (c+50, 50)),
(0, U): (5, U, lambda r, c: (199, c-100)), # d
(5, D): (0, D, lambda r, c: (0, c+100)),
(1, L): (4, R, lambda r, c: (149-r, 0)), # e
(4, L): (1, R, lambda r, c: (149-r, 50)),
(3, D): (5, L, lambda r, c: (c+100, 49)), # f
(5, R): (3, U, lambda r, c: (149, r-100)),
(1, U): (5, R, lambda r, c: (c+100, 0)), # g
(5, L): (1, D, lambda r, c: (0, r-100))
}
def next_pos(r, c, dir):
nr, nc, ndir = r+directions[dir][0], c+directions[dir][1], dir
if face_number(nr, nc) == None:
_, ndir, xform = edges[(face_number(r, c), dir)]
nr, nc = xform(r, c)
if map[nr][nc] == WALL:
return (r, c, dir)
else:
return (nr, nc, ndir)
solve()