This repository has been archived by the owner on Oct 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_22.py
222 lines (199 loc) · 9.96 KB
/
day_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
import re
from enum import IntEnum
from pathlib import Path
from typing import List, Tuple
from constants import INPUTS_DIR, UTF_8
INPUT_PATH = Path(INPUTS_DIR) / "day-22.txt"
# INPUT_PATH = Path(INPUTS_DIR) / "example.txt"
VOID = " "
WALL = "#"
OPEN = "."
CLOCKWISE = "R"
COUNTER_CLOCKWISE = "L"
CUBE_FACE_SIZE = 50
class Direction(IntEnum):
RIGHT = 0
DOWN = 1
LEFT = 2
UP = 3
def rot_clockwise(d: Direction) -> Direction:
return Direction((d + 1) % 4)
def rot_counter_clockwise(d: Direction) -> Direction:
return Direction((d - 1) % 4)
def score_position(row: int, col: int, d: Direction) -> int:
return (1000 * (row + 1)) + (4 * (col + 1)) + d
def main(maze: List[str], instructions: List[str]) -> int:
row = 0
col = maze[row].index(OPEN)
d = Direction.RIGHT
for step in instructions:
if step == CLOCKWISE:
d = rot_clockwise(d)
elif step == COUNTER_CLOCKWISE:
d = rot_counter_clockwise(d)
else: # int
n_moves = int(step)
for _ in range(n_moves):
if d == Direction.RIGHT:
if col + 1 >= len(maze[row]) or maze[row][col + 1] == VOID:
for first_col in range(col):
if maze[row][first_col] != VOID:
next_col = first_col
break
else:
raise ValueError("never found the first non-VOID char of the line")
else:
next_col = col + 1
next_row = row
elif d == Direction.LEFT:
if col - 1 < 0 or maze[row][col - 1] == VOID:
for last_col in range(len(maze[row]) - 1, col, -1):
if maze[row][last_col] != VOID:
next_col = last_col
break
else:
raise ValueError("never found the last non-VOID char of the line")
else:
next_col = col - 1
next_row = row
elif d == Direction.DOWN:
if row + 1 >= len(maze) or maze[row + 1][col] == VOID:
for first_row in range(row):
if maze[first_row][col] != VOID:
next_row = first_row
break
else:
raise ValueError("never found the first non-VOID char of the column")
else:
next_row = row + 1
next_col = col
elif d == Direction.UP:
if row - 1 < 0 or maze[row - 1][col] == VOID:
for last_row in range(len(maze) - 1, row, -1):
if maze[last_row][col] != VOID:
next_row = last_row
break
else:
raise ValueError("never found the last non-VOID char of the column")
else:
next_row = row - 1
next_col = col
else:
raise ValueError(f"unexpected direction enum: {d}")
# check the val of the next col
if maze[next_row][next_col] == OPEN:
row = next_row
col = next_col
elif maze[next_row][next_col] == WALL:
break # no more moves in this direction
else:
raise ValueError(f"expected open or wall, but found {repr(maze[next_row][next_col])}")
return score_position(row, col, d)
class CubeMaze:
def __init__(self, maze: List[str]):
self.maze = maze
self._validate()
def _validate(self):
for row in range(2 * CUBE_FACE_SIZE):
for col in range(CUBE_FACE_SIZE):
if self.maze[row][col] != VOID:
raise ValueError("unfolded cube maze did not take expected form")
for row in range(2 * CUBE_FACE_SIZE, 4 * CUBE_FACE_SIZE):
for col in range(CUBE_FACE_SIZE):
if self.maze[row][col] not in (OPEN, WALL):
raise ValueError("unfolded cube maze did not take expected form")
for row in range(3 * CUBE_FACE_SIZE):
for col in range(CUBE_FACE_SIZE, 2 * CUBE_FACE_SIZE):
if self.maze[row][col] not in (OPEN, WALL):
raise ValueError("unfolded cube maze did not take expected form")
for row in range(3 * CUBE_FACE_SIZE, 4 * CUBE_FACE_SIZE):
for col in range(CUBE_FACE_SIZE, 2 * CUBE_FACE_SIZE):
if self.maze[row][col] != VOID:
raise ValueError("unfolded cube maze did not take expected form")
for row in range(CUBE_FACE_SIZE):
for col in range(2 * CUBE_FACE_SIZE, 3 * CUBE_FACE_SIZE):
if self.maze[row][col] not in (OPEN, WALL):
raise ValueError("unfolded cube maze did not take expected form")
for row in range(CUBE_FACE_SIZE, 4 * CUBE_FACE_SIZE):
for col in range(2 * CUBE_FACE_SIZE, 3 * CUBE_FACE_SIZE):
if self.maze[row][col] != VOID:
raise ValueError("unfolded cube maze did not take expected form")
def cube_step(self, row: int, col: int, d: Direction) -> Tuple[int, int, Direction]:
if row == 0 and CUBE_FACE_SIZE <= col < 2 * CUBE_FACE_SIZE and d == Direction.UP:
return (3 * CUBE_FACE_SIZE) + (col - CUBE_FACE_SIZE), 0, Direction.RIGHT
elif row == 0 and 2 * CUBE_FACE_SIZE <= col < 3 * CUBE_FACE_SIZE and d == Direction.UP:
return 4 * CUBE_FACE_SIZE - 1, col - (2 * CUBE_FACE_SIZE), Direction.UP
elif 0 <= row < CUBE_FACE_SIZE and col == CUBE_FACE_SIZE and d == Direction.LEFT:
return (3 * CUBE_FACE_SIZE - 1) - row, 0, Direction.RIGHT
elif 0 <= row < CUBE_FACE_SIZE and col == 3 * CUBE_FACE_SIZE - 1 and d == Direction.RIGHT:
return (3 * CUBE_FACE_SIZE - 1) - row, 2 * CUBE_FACE_SIZE - 1, Direction.LEFT
elif row == CUBE_FACE_SIZE - 1 and 2 * CUBE_FACE_SIZE <= col < 3 * CUBE_FACE_SIZE and d == Direction.DOWN:
return CUBE_FACE_SIZE + (col - 2 * CUBE_FACE_SIZE), 2 * CUBE_FACE_SIZE - 1, Direction.LEFT
elif CUBE_FACE_SIZE <= row < 2 * CUBE_FACE_SIZE and col == CUBE_FACE_SIZE and d == Direction.LEFT:
return 2 * CUBE_FACE_SIZE, row - CUBE_FACE_SIZE, Direction.DOWN
elif CUBE_FACE_SIZE <= row < 2 * CUBE_FACE_SIZE and col == 2 * CUBE_FACE_SIZE - 1 and d == Direction.RIGHT:
return CUBE_FACE_SIZE - 1, (2 * CUBE_FACE_SIZE) + (row - CUBE_FACE_SIZE), Direction.UP
elif row == 2 * CUBE_FACE_SIZE and 0 <= col < CUBE_FACE_SIZE and d == Direction.UP:
return CUBE_FACE_SIZE + col, CUBE_FACE_SIZE, Direction.RIGHT
elif 2 * CUBE_FACE_SIZE <= row < 3 * CUBE_FACE_SIZE and col == 0 and d == Direction.LEFT:
return (3 * CUBE_FACE_SIZE - 1) - row, CUBE_FACE_SIZE, Direction.RIGHT
elif 2 * CUBE_FACE_SIZE <= row < 3 * CUBE_FACE_SIZE and col == 2 * CUBE_FACE_SIZE - 1 and d == Direction.RIGHT:
return (3 * CUBE_FACE_SIZE - 1) - row, 3 * CUBE_FACE_SIZE - 1, Direction.LEFT
elif row == 3 * CUBE_FACE_SIZE - 1 and CUBE_FACE_SIZE <= col < 2 * CUBE_FACE_SIZE and d == Direction.DOWN:
return (3 * CUBE_FACE_SIZE) + (col - CUBE_FACE_SIZE), CUBE_FACE_SIZE - 1, Direction.LEFT
elif 3 * CUBE_FACE_SIZE <= row < 4 * CUBE_FACE_SIZE and col == 0 and d == Direction.LEFT:
return 0, CUBE_FACE_SIZE + (row - 3 * CUBE_FACE_SIZE), Direction.DOWN
elif 3 * CUBE_FACE_SIZE <= row < 4 * CUBE_FACE_SIZE and col == CUBE_FACE_SIZE - 1 and d == Direction.RIGHT:
return 3 * CUBE_FACE_SIZE - 1, CUBE_FACE_SIZE + (row - 3 * CUBE_FACE_SIZE), Direction.UP
elif row == 4 * CUBE_FACE_SIZE - 1 and 0 <= col < CUBE_FACE_SIZE and d == Direction.DOWN:
return 0, (2 * CUBE_FACE_SIZE) + col, Direction.DOWN
else:
match d:
case Direction.RIGHT:
return row, col + 1, d
case Direction.DOWN:
return row + 1, col, d
case Direction.LEFT:
return row, col - 1, d
case Direction.UP:
return row - 1, col, d
case _:
raise ValueError(f"unexpected direction enum: {d}")
def main2(maze: List[str], instructions: List[str]) -> int:
cube = CubeMaze(maze)
row = 0
col = maze[row].index(OPEN)
d = Direction.RIGHT
for step in instructions:
if step == CLOCKWISE:
d = rot_clockwise(d)
elif step == COUNTER_CLOCKWISE:
d = rot_counter_clockwise(d)
else: # int
n_moves = int(step)
for _ in range(n_moves):
next_row, next_col, next_d = cube.cube_step(row, col, d)
# check the val of the next spot
if maze[next_row][next_col] == OPEN:
row = next_row
col = next_col
d = next_d
elif maze[next_row][next_col] == WALL:
break # no more moves in this direction
else:
raise ValueError(f"expected open or wall, but found {repr(maze[next_row][next_col])}")
return score_position(row, col, d)
if __name__ == "__main__":
with open(INPUT_PATH, "r", encoding=UTF_8) as f:
maze_, instructions_ = f.read().split("\n\n")
maze_ = maze_.split("\n")
maze_width = max(len(row_) for row_ in maze_)
maze_ = [
row_ + (" " * (maze_width - len(row_)))
for row_ in maze_
]
instructions_ = re.findall(r"\d+|[LR]", instructions_.strip())
ans = main(maze_, instructions_)
print("part 1:", ans)
ans = main2(maze_, instructions_)
print("part 2:", ans)