Advertisement
Guest User

YOM2 - Advent of Code 2024 Day 15

a guest
Dec 15th, 2024
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | None | 0 0
  1. with open('input/Day15.txt', 'r') as file:
  2.     room, move = file.read().split('\n\n')
  3.  
  4. move = move.replace('\n', '')
  5. walls = []
  6. boxes = []
  7. wide_boxes = []
  8.  
  9. for line in room.splitlines():
  10.     cur_walls = []
  11.     cur_boxes = []
  12.  
  13.     cur_wide_boxes = []
  14.     for char in line:
  15.         if char == '#':
  16.             cur_walls.append(True)
  17.             cur_boxes.append(False)
  18.  
  19.             cur_wide_boxes += [None, None]
  20.         elif char == 'O':
  21.             cur_walls.append(False)
  22.             cur_boxes.append(True)
  23.  
  24.             cur_wide_boxes += [1, -1]
  25.         else:
  26.             if char == '@':
  27.                 x = x2 = len(walls)
  28.                 y = len(cur_walls)
  29.                 y2 = len(cur_wide_boxes)
  30.             cur_walls.append(False)
  31.             cur_boxes.append(False)
  32.  
  33.             cur_wide_boxes += [None, None]
  34.     walls.append(cur_walls)
  35.     boxes.append(cur_boxes)
  36.     wide_boxes.append(cur_wide_boxes)
  37.  
  38. directions = {
  39.     '^' : (-1, 0),
  40.     '>' : ( 0, 1),
  41.     'v' : ( 1, 0),
  42.     '<' : ( 0,-1)
  43.     }
  44.  
  45. # Part 1
  46.  
  47. for char in move:
  48.     dx, dy = directions[char]
  49.     i = 1
  50.     while boxes[x + i*dx][y + i*dy]:
  51.         i += 1
  52.     if not walls[x + i*dx][y + i*dy]:
  53.         boxes[x + i*dx][y + i*dy] = boxes[x + dx][y + dy]
  54.         boxes[x + dx][y + dy] = False
  55.         x += dx
  56.         y += dy
  57.  
  58. gps = 0
  59. for i, row in enumerate(boxes):
  60.     for j, box in enumerate(row):
  61.         if box:
  62.             gps += 100*i + j
  63. print(gps)
  64.  
  65. # Part 2
  66.  
  67. x, y = x2, y2
  68.  
  69. def canMove(x,y,dx):
  70.     dy = wide_boxes[x][y]
  71.     if dy:
  72.         return canMove(x+dx,y,dx) and canMove(x+dx,y+dy,dx)
  73.     return not walls[x][y//2]
  74.  
  75. def moveBoxes(x,y,dx):
  76.     dy = wide_boxes[x][y]
  77.     if dy:
  78.         moveBoxes(x+dx,y,dx)
  79.         moveBoxes(x+dx,y+dy,dx)
  80.         wide_boxes[x+dx][y] = wide_boxes[x][y]
  81.         wide_boxes[x+dx][y+dy] = wide_boxes[x][y+dy]
  82.         wide_boxes[x][y] = None
  83.         wide_boxes[x][y+dy] = None
  84.        
  85.  
  86. for char in move:
  87.     dx, dy = directions[char]
  88.     if dx:
  89.         if canMove(x+dx,y,dx):
  90.             moveBoxes(x+dx,y,dx)
  91.             x += dx
  92.     else:
  93.         i = 1
  94.         while wide_boxes[x][y+i*dy]:
  95.             i += 1
  96.         if not walls[x][(y+i*dy)//2]:
  97.             wide_boxes[x].insert(y, wide_boxes[x].pop(y+i*dy))
  98.             y += dy
  99.  
  100. gps = 0
  101. for i, row in enumerate(wide_boxes):
  102.     for j, box in enumerate(row):
  103.         if box == 1:
  104.             gps += 100*i + j
  105. print(gps)
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement