-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_15.py
320 lines (254 loc) · 7.39 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
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
from utils import read_input, pprint, config
from pathlib import Path
testing = False
config['verbose'] = False
current_file = "day_15"
data_path = Path(f'./data/{current_file}/input.txt')
if not testing:
data = read_input(data_path)
else:
data = '''
##########
#..O..O.O#
#......O.#
#.OO..O.O#
#..O@..O.#
#O#..O...#
#O..O..O.#
#.OO.O.OO#
#....O...#
##########
<vv>^<v^>v>^vv^v>v<>v^v<v<^vv<<<^><<><>>v<vvv<>^v^>^<<<><<v<<<v^vv^v>^
vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<<v<^v>^<^^>>>^<v<v
><>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^<v>v^^<^^vv<
<<v<^>>^^^^>>>v^<>vvv^><v<<<>^^^vv^<vvv>^>v<^^^^v<>^>vvvv><>>v^<<^^^^^
^><^><>>><>^^<<^^v>>><^<v>^<vv>>v>>>^v><>^v><<<<v>>v<v<v>vvv>^<><<>^><
^>><>^v<><^vvv<^^<><v<<<<<><^v<<<><<<^^<v<^^^><^>>^<v^><<<^>>^v<v^v<v^
>^>>^v>vv>^<<^v<>><<><<v<<v><>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^
<><^^>^^^<><vvvvv^v<v<<>^v<v>v<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<>
^^>vv<^v^v<vv>^<><v<^v>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<><<v>
v^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v><^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^
'''
# data = '''
# #######
# #...#.#
# #.....#
# #..OO@#
# #..O..#
# #.....#
# #######
# <vv<<^^<<^^
# '''
[grid_string, move_string] = data.split('\n\n')
bot = None
grid = {}
sg = grid_string.split()
nbRows = len(sg)
nbCols = len(sg[0])
for row, r in enumerate(sg):
if not r:
continue
for col, s in enumerate(r):
pos = (row, col)
if s == '@':
bot = pos
grid[pos] = s
def print_grid(grid):
for r in range(nbRows):
l = ''
for c in range(nbCols):
l += grid[(r, c)]
pprint(l)
moves = ''.join(move_string.split('\n'))
move_map = {
'<': (0, -1),
'v': (1, 0),
'^': (-1, 0),
'>': (0, 1),
}
def move_block(grid, pos, shift):
new_pos = pos[0] + shift[0], pos[1] + shift[1]
sym = grid[pos]
if grid[new_pos] == '#':
return False, grid, pos
if grid[new_pos] == '.':
grid[new_pos] = sym
grid[pos] = "."
return True, grid, new_pos
# new pos contains a o
has_moved, grid, _ = move_block(grid, new_pos, shift)
if has_moved:
grid[new_pos] = sym
grid[pos] = "."
return True, grid, new_pos
return False, grid, pos
for move in moves:
# break
shift = move_map[move]
moved, grid, bot = move_block(grid, bot, shift)
# pprint('move', move)
# pprint_grid(grid)
# pprint()
s = 0
for pos, sym in grid.items():
if sym == "O":
s += 100*pos[0]+pos[1]
solution = s
print(f'Part 1 - solution: {solution}')
# Part 2
class Box():
def __init__(self, pos):
self.left = pos
self.right = (pos[0], pos[1]+1)
def __str__(self):
return f"{self.left}-{self.right}"
def __repr__(self):
return f"{self.left}-{self.right}"
def new_pos(self, dir):
shift = move_map[dir]
new_left = self.left[0] + shift[0], self.left[1] + shift[1]
new_right = self.right[0] + shift[0], self.right[1] + shift[1]
return new_left, new_right
def nghb(self, grid, dir):
new_left, new_right = self.new_pos(dir)
left_element = grid[new_left]
right_element = grid[new_right]
n = [
False if left_element == "#" else None,
False if right_element == "#" else None,
]
if left_element not in ['.', '#']:
n[0] = None if left_element == self else left_element
if right_element not in ['.', '#']:
n[1] = None if right_element == self else right_element
return n
def apply_move(self, grid, dir):
new_left, new_right = self.new_pos(dir)
grid[self.left] = '.'
grid[self.right] = '.'
grid[new_left] = self
grid[new_right] = self
self.left = new_left
self.right = new_right
def move(self, grid, dir):
new_left, new_right = self.new_pos(dir)
if grid[new_left] == '#' or grid[new_right] == '#':
return False, grid
if (
grid[new_left] == '.' or grid[new_left] == self
) and (
grid[new_right] == '.' or grid[new_right] == self
):
self.apply_move(grid, dir)
return True, grid
else:
# we hit another box
nghb_levels = [set(self.nghb(grid, dir))]
if False in nghb_levels:
return False, grid
pprint('level1', nghb_levels)
# detect all neighbours
while True:
new_nghb = set()
for n in nghb_levels[-1]:
if n is None:
continue
pprint('start_element', n)
cur = n.nghb(grid, dir)
pprint('Box', n, 'neighbours:')
pprint(cur)
if False in cur:
return False, grid
for c in cur:
new_nghb.add(c)
if len(new_nghb) <= 0:
break
nghb_levels.append(new_nghb)
# no false returned => all can move
while len(nghb_levels):
level = nghb_levels.pop()
for n in level:
if n is None:
continue
n.apply_move(grid, dir)
self.apply_move(grid, dir)
return True, grid
def move_bot(grid, pos, shift, dir):
new_pos = pos[0] + shift[0], pos[1] + shift[1]
sym = grid[pos]
if grid[new_pos] == '#':
return False, grid, pos
if grid[new_pos] == '.':
grid[new_pos] = sym
grid[pos] = "."
return True, grid, new_pos
# new pos contains a box
box = grid[new_pos]
# self = box
has_moved, grid = box.move(grid, dir)
if has_moved:
grid[new_pos] = sym
grid[pos] = "."
return True, grid, new_pos
return False, grid, pos
def print_box_grid(grid):
for r in range(nbRows):
l = ''
for c in range(nbCols):
el = grid[(r, c)]
if el in [".", "@", '#']:
l += el
else:
# in a box
if (r, c) == el.left:
l += '['
else:
l += ']'
pprint(l)
bot = None
grid = {}
sg = grid_string.split()
nbRows = len(sg)
nbCols = 2*len(sg[0])
allBoxes = []
for row, r in enumerate(sg):
if not r:
continue
for col, s in enumerate(r):
pos1 = (row, 2*col)
pos2 = (row, 2*col+1)
if s == '@':
bot = pos1
grid[pos1] = s
grid[pos2] = '.'
elif s == '.':
grid[pos1] = s
grid[pos2] = s
elif s == '#':
grid[pos1] = s
grid[pos2] = s
elif s == 'O':
b = Box(pos1)
grid[pos1] = b
grid[pos2] = b
allBoxes.append(b)
else:
print('** element', s)
raise Exception('unknown grid element')
print_box_grid(grid)
for i, move in enumerate(moves):
# if (i == 156):
# break
shift = move_map[move]
# pos=bot
# dir=move
pprint('move', i, ':', move)
moved, grid, bot = move_bot(grid, bot, shift, move)
print_box_grid(grid)
pprint()
print_box_grid(grid)
s = 0
for b in allBoxes:
s += 100*b.left[0] + b.left[1]
solution = s
print(f'Part 2 - solution: {solution}')