-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-15.py
231 lines (188 loc) · 6.5 KB
/
day-15.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
import sys
import aocd
from dotenv import load_dotenv
from tqdm import tqdm
load_dotenv()
# Set the day and year
DAY = "15"
YEAR = "2024"
# Icons
ROBOT = "@"
WALL = "#"
EMPTY = "."
BOX = "O"
BOX_LEFT = "["
BOX_RIGHT = "]"
# Moves
MOVES = {">": (0, 1), "<": (0, -1), "^": (-1, 0), "v": (1, 0)}
def get_input(path):
"""Load the data from the file"""
# Open the file
with open(f"{path}/day-{DAY}.txt", "r") as f:
warehouse_map, directions = f.read().split("\n\n")
# Tidy up
warehouse_map = [list(line) for line in warehouse_map.split("\n")]
directions = directions.replace("\n", "")
return warehouse_map, directions
def find_robot(warehouse_map):
# Find the robot starting point
for row_i, row in enumerate(warehouse_map):
for col_j, value in enumerate(row):
if value == ROBOT:
return (row_i, col_j)
def print_grid(warehouse_map):
for line in warehouse_map:
print("".join(line))
def calculate_coordinates(warehouse_map, box=BOX):
answer = 0
for row_i, row in enumerate(warehouse_map):
for col_j, value in enumerate(row):
if value == box:
answer += 100 * row_i + col_j
return answer
def part_1(path, submit):
"""Part 1/Star 1"""
# Get the data
warehouse_map, directions = get_input(path)
# Find the robot starting point
robot = find_robot(warehouse_map)
# Remove the robot - no longer required
warehouse_map[robot[0]][robot[1]] = EMPTY
# Move the robot
for move in tqdm(directions):
new_row, new_col = (
robot[0] + MOVES[move][0],
robot[1] + MOVES[move][1],
)
if warehouse_map[new_row][new_col] == EMPTY:
# Clear space, so robot moves
robot = (new_row, new_col)
elif warehouse_map[new_row][new_col] == WALL:
# Wall - don't move
pass
else:
# Box, need to work out whether to move the box
# Find the end of the line of boxes
next_row, next_col = new_row, new_col
while warehouse_map[next_row][next_col] == BOX:
next_row += MOVES[move][0]
next_col += MOVES[move][1]
# Need to check the end of the line of boxes
if warehouse_map[next_row][next_col] == EMPTY:
# Boxes can move
warehouse_map[next_row][next_col] = BOX
warehouse_map[new_row][new_col] = EMPTY
# Move the robot
robot = (new_row, new_col)
else:
# Boxes can't move, so nothing happens
pass
# Print the grid
print_grid(warehouse_map)
# Get GPS coordinates of boxes
answer = calculate_coordinates(warehouse_map, box=BOX)
# Print out the response
print(f"Task 1 Answer: {answer}")
# Submit the answer
if submit:
aocd.submit(answer, part="a", day=int(DAY), year=int(YEAR))
def part_2(path, submit):
"""Part 2/Star 2"""
# Get the data
original_warehouse_map, directions = get_input(path)
# Double the spaces
warehouse_map = []
for row in original_warehouse_map:
new_row = []
for space in row:
if space in (EMPTY, WALL):
new_row += [space, space]
elif space == ROBOT:
new_row += [ROBOT, EMPTY]
else:
new_row += [BOX_LEFT, BOX_RIGHT]
warehouse_map.append(new_row)
# Find the robot starting point
robot = find_robot(warehouse_map)
# Print the grid
print_grid(warehouse_map)
# Move the robot
for move in tqdm(directions):
to_move = {robot}
stack = [robot]
while stack:
row, col = stack.pop()
# "Move" the item
row += MOVES[move][0]
col += MOVES[move][1]
if (row, col) not in to_move:
if warehouse_map[row][col] in [BOX_LEFT, BOX_RIGHT]:
to_move.add((row, col))
stack.append((row, col))
if warehouse_map[row][col] == BOX_LEFT:
# Need to move the other half of the box
to_move.add((row, col + 1))
stack.append((row, col + 1))
elif warehouse_map[row][col] == BOX_RIGHT:
to_move.add((row, col - 1))
stack.append((row, col - 1))
# Check everything can move
if all(
[
warehouse_map[row + MOVES[move][0]][col + MOVES[move][1]]
!= WALL
for (row, col) in to_move
]
):
new_grid = [[EMPTY for _ in row] for row in warehouse_map]
# Add the new item locations
for row, col in to_move:
new_grid[row + MOVES[move][0]][col + MOVES[move][1]] = (
warehouse_map[row][col]
)
# Add the old item locations
for row_i in range(len(warehouse_map)):
for col_j in range(len(warehouse_map[row_i])):
if (row_i, col_j) not in to_move and new_grid[row_i][
col_j
] == EMPTY:
new_grid[row_i][col_j] = warehouse_map[row_i][col_j]
warehouse_map = new_grid
# Move the robot
robot = (robot[0] + MOVES[move][0], robot[1] + MOVES[move][1])
# Print the grid
print_grid(warehouse_map)
# Get GPS coordinates of boxes
answer = calculate_coordinates(warehouse_map, box=BOX_LEFT)
# Print out the response
print(f"Task 2 Answer: {answer}")
# Submit the answer
if submit:
aocd.submit(answer, part="b", day=int(DAY), year=int(YEAR))
if __name__ == "__main__":
"""
Run using e.g.
`python day-15.py -test`
`python day-15.py`
`python day-15.py -submit`
`python day-15.py -test -2`
`python day-15.py -2`
`python day-15.py -test -both`
`python day-15.py -both`
"""
# Identify the folder that the input is in
test = "-test" in sys.argv
if test:
path = "input-tests"
else:
path = "inputs"
# Identify if they need to submit the answer
submit = "-test" not in sys.argv and "-submit" in sys.argv
# Identify which one to run - 1 is default
if "-2" in sys.argv:
part_2(path, submit)
elif "-both" in sys.argv:
part_1(path, submit)
part_2(path, submit)
else:
part_1(path, submit)