Advent of code

 15 décembre 2024 

  Warehouse Woes : Déplacer des caisses façon sokoban, mais avec des caisses prenant deux places en largeur.
##########  ###
   
##.#...OO#  O.#
   
#O#O.....O  ..#
   
        ⋮   
#..O.O.O..  #O#
   
#.....O.OO  OO#
   
##########  ###
   
  
   
<v><v><>  
   
>^><<>vv  
   
        
Toute petite optimisation possible (qui ne fait gagner aucun temps) quand on touche une boîte horizontalement : s'occuper directement de la partie non touchée, qu'on sait être derrière.
  1. code_part_1_2.py
  2. code_part1_simple_affichageConsole.py
  3. code_part2_affichageConsole.py
  4. diff_adaptationAlgo_2_PourPartie_1.diffy
  5. diff_optimPushingHorizontally.diffy
  6. affichage_console_partie1.png
  7. affichage_console_partie2.png
  8. Visualisation superbe sur YouTube En 3D avec images réalistes et faite par Paul Bonsma
##### 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' )  )