########## … ###
##.#...OO# … O.#
#O#O.....O … ..#
#..O.O.O.. … #O#
#.....O.OO … OO#
########## … ###
<v><v><> …
>^><<>vv …
##### DÉBUT ALGO ##### ##### DÉBUT ALGO ##### # algo 2e partie un peu overkill pour
def next_grid(grid, x, y, depl_x, depl_y): def next_grid(grid, x, y, depl_x, depl_y):
### Find what needs to move ### Find what needs to move
pushed = set() pushed = set()
todo = set([(x, y)]) todo = set([(x, y)])
while todo: while todo:
bx, by = todo.pop() bx, by = todo.pop()
pushed.add( (bx,by) ) pushed.add( (bx,by) )
nx, ny = bx + depl_x, by + depl_y nx, ny = bx + depl_x, by + depl_y
if (nx,ny) in pushed or (nx,ny) in todo: if (nx,ny) in pushed or (nx,ny) in todo:
continue continue
next_c = grid[ny][nx] next_c = grid[ny][nx]
if next_c == '#': if next_c == '#':
return None # Obstacle ! Nothing to do, retu return None # Obstacle ! Nothing to do, retu
elif next_c in '[]': | elif next_c in '[]O':
todo.add( (nx, ny) ) # Always process the todo.add( (nx, ny) ) # Always process the touched part of the box
> if next_c in '[]': # If box has two parts
if not depl_y: # If pushing horizont if not depl_y: # If pushing horizontally
todo.add( (bx+2*depl_x, by) ) # process othe todo.add( (bx+2*depl_x, by) ) # process other part
elif next_c == '[': # If pushing vertical elif next_c == '[': # If pushing vertically on the left
todo.add( (bx+1,ny) ) # process the ri todo.add( (bx+1,ny) ) # process the right part of the box
else: # If pushing vertical else: # If pushing vertically on the right
todo.add( (bx-1,ny) ) # process the le todo.add( (bx-1,ny) ) # process the left part
### And push everything! ### And push everything!
... ...
print("Réponse partie 2:", sum( 100*y + x print("Réponse {part}:", sum( 100*y + x
for y in range(len(grid)) for y in range(len(grid))
for x in range(len(grid[0])) for x in range(len(grid[0]))
if grid[y][x] == '[' ) ) | if grid[y][x] in '[O' ) )