Advertisement
CrayonCrayoff

AoC 2024 Day 15

Dec 15th, 2024
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.63 KB | None | 0 0
  1. # output is sum of each box's y index * 100 + x index
  2.  
  3. # part 1
  4. grid = []
  5. moves = []
  6. with open("day15input.txt") as f:
  7.     input_lines = f.read().splitlines()
  8.     first_part = True
  9.     for line in input_lines:
  10.         if not line:
  11.             first_part = False
  12.             continue
  13.         if first_part:
  14.             grid.append(list(line))
  15.         else:
  16.             moves.append(line)
  17.     moves = "".join(moves)
  18.  
  19. def sum_gps_coordinates(matrix: list[list[str]], instructions: str) -> int:
  20.     step = {'<': (0, -1), 'v': (1, 0), '>': (0, 1), '^': (-1, 0)}
  21.  
  22.     rows = len(matrix)
  23.     cols = len(matrix[0])
  24.  
  25.     curr_pos = (-1, -1)
  26.     for y in range(rows):
  27.         for x in range(cols):
  28.             if matrix[y][x] == '@':
  29.                 curr_pos = (y, x)
  30.                 break
  31.         if curr_pos != (-1, -1):
  32.             break
  33.  
  34.     for i in instructions:
  35.         y1, x1 = curr_pos
  36.         dy, dx = step[i]
  37.         y2, x2 = y1 + dy, x1 + dx  # this is the adjacent tile in the same direction
  38.  
  39.         if matrix[y2][x2] == "O":
  40.             y3, x3 = y2 + dy, x2 + dx  # this is the next tile in the same direction
  41.             while matrix[y3][x3] != '#':
  42.                 if matrix[y3][x3] == '.':
  43.                     matrix[y2][x2], matrix[y3][x3] = matrix[y3][x3], matrix[y2][x2]
  44.                     break
  45.                 y3, x3 = y3 + dy, x3 + dx
  46.         if matrix[y2][x2] == '.':
  47.             matrix[y1][x1], matrix[y2][x2] = matrix[y2][x2], matrix[y1][x1]
  48.             curr_pos = y2, x2
  49.  
  50.     # after all moves, calculate output
  51.     output = 0
  52.     for y in range(rows):
  53.         for x in range(cols):
  54.             if matrix[y][x] == 'O':
  55.                 output += y * 100 + x
  56.  
  57.     return output
  58.  
  59.  
  60. print(sum_gps_coordinates(grid, moves))
  61.  
  62.  
  63.  
  64. # part 2
  65. wide_grid = []
  66. moves = []
  67. with open("day15input.txt") as f:
  68.     input_lines = f.read().splitlines()
  69.     first_part = True
  70.     for line in input_lines:
  71.         if not line:
  72.             first_part = False
  73.         elif not first_part:
  74.             moves.append(line)
  75.         elif first_part:
  76.             temp = []
  77.             for char in line:
  78.                 if char == "O":
  79.                     temp.extend(["[", "]"])
  80.                 elif char == "#":
  81.                     temp.extend(["#"] * 2)
  82.                 elif char == ".":
  83.                     temp.extend(["."] * 2)
  84.                 elif char == "@":
  85.                     temp.extend(["@", "."])
  86.             wide_grid.append(temp)
  87.     moves = "".join(moves)
  88.  
  89.  
  90. def sum_gps_coordinates2(matrix: list[list[str]], instructions: str) -> int:
  91.     def push(stack: list[list[tuple[int, int]]], direction: str):
  92.         nonlocal matrix, step
  93.         offset = step[direction]
  94.         if direction == "<" or direction == ">":
  95.             stack = stack[0]
  96.             y3, x3 = stack[-1]
  97.             while matrix[y3][x3] != '#' and matrix[y3][x3] != '.':
  98.                 x3 += offset[1]
  99.                 stack.append((y3, x3))
  100.             if matrix[y3][x3] == '#':
  101.                 return
  102.             else:
  103.                 curr_y, curr_x = y3, x3
  104.                 while stack:
  105.                     next_y, next_x = stack.pop()
  106.                     matrix[curr_y][curr_x], matrix[next_y][next_x] = matrix[next_y][next_x], matrix[curr_y][curr_x]
  107.                     curr_y, curr_x = next_y, next_x
  108.                 return
  109.         elif direction == '^' or direction == 'v':
  110.             temp = {(1, 1)}
  111.             while temp:
  112.                 temp = set()
  113.                 for coord in stack[-1]:
  114.                     if matrix[coord[0] + offset[0]][coord[1]] == '#':
  115.                         return
  116.                     if matrix[coord[0] + offset[0]][coord[1]] == '[':
  117.                         temp.add((coord[0] + offset[0], coord[1]))
  118.                         temp.add((coord[0] + offset[0], coord[1] + 1))
  119.                     elif matrix[coord[0] + offset[0]][coord[1]] == ']':
  120.                         temp.add((coord[0] + offset[0], coord[1]))
  121.                         temp.add((coord[0] + offset[0], coord[1] - 1))
  122.                 stack.append(list(temp))
  123.             stack.pop()
  124.             while stack:
  125.                 curr_tiles = stack.pop()
  126.                 for tile in curr_tiles:
  127.                     (matrix[tile[0]][tile[1]],
  128.                      matrix[tile[0]+offset[0]][tile[1]]) = (matrix[tile[0]+offset[0]][tile[1]],
  129.                                                             matrix[tile[0]][tile[1]])
  130.         return
  131.  
  132.     step = {'<': (0, -1), 'v': (1, 0), '>': (0, 1), '^': (-1, 0)}
  133.  
  134.     rows = len(matrix)
  135.     cols = len(matrix[0])
  136.  
  137.     # find starting position
  138.     curr_pos = (-1, -1)
  139.     for y in range(rows):
  140.         for x in range(cols):
  141.             if matrix[y][x] == '@':
  142.                 curr_pos = (y, x)
  143.                 break
  144.         if curr_pos != (-1, -1):
  145.             break
  146.  
  147.     # execute instructions
  148.     for i in instructions:
  149.         y1, x1 = curr_pos
  150.         dy, dx = step[i]
  151.         y2, x2 = y1 + dy, x1 + dx  # this is the adjacent tile in the current direction
  152.         if matrix[y2][x2] == "[":
  153.             push([[(y2, x2), (y2, x2+1)]], i)
  154.         elif matrix[y2][x2] == "]":
  155.             temp = []
  156.             temp.append([(y2, x2), (y2, x2-1)])
  157.             push([[(y2, x2), (y2, x2-1)]], i)
  158.         if matrix[y2][x2] == '.':
  159.             matrix[y1][x1], matrix[y2][x2] = matrix[y2][x2], matrix[y1][x1]
  160.             curr_pos = y2, x2
  161.  
  162.     # after all moves, calculate output
  163.     output = 0
  164.     for y in range(rows):
  165.         for x in range(cols):
  166.             if matrix[y][x] == '[':
  167.                 output += y * 100 + x
  168.  
  169.     return output
  170.  
  171.  
  172. print(sum_gps_coordinates2(wide_grid, moves))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement