Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # output is sum of each box's y index * 100 + x index
- # part 1
- grid = []
- moves = []
- with open("day15input.txt") as f:
- input_lines = f.read().splitlines()
- first_part = True
- for line in input_lines:
- if not line:
- first_part = False
- continue
- if first_part:
- grid.append(list(line))
- else:
- moves.append(line)
- moves = "".join(moves)
- def sum_gps_coordinates(matrix: list[list[str]], instructions: str) -> int:
- step = {'<': (0, -1), 'v': (1, 0), '>': (0, 1), '^': (-1, 0)}
- rows = len(matrix)
- cols = len(matrix[0])
- curr_pos = (-1, -1)
- for y in range(rows):
- for x in range(cols):
- if matrix[y][x] == '@':
- curr_pos = (y, x)
- break
- if curr_pos != (-1, -1):
- break
- for i in instructions:
- y1, x1 = curr_pos
- dy, dx = step[i]
- y2, x2 = y1 + dy, x1 + dx # this is the adjacent tile in the same direction
- if matrix[y2][x2] == "O":
- y3, x3 = y2 + dy, x2 + dx # this is the next tile in the same direction
- while matrix[y3][x3] != '#':
- if matrix[y3][x3] == '.':
- matrix[y2][x2], matrix[y3][x3] = matrix[y3][x3], matrix[y2][x2]
- break
- y3, x3 = y3 + dy, x3 + dx
- if matrix[y2][x2] == '.':
- matrix[y1][x1], matrix[y2][x2] = matrix[y2][x2], matrix[y1][x1]
- curr_pos = y2, x2
- # after all moves, calculate output
- output = 0
- for y in range(rows):
- for x in range(cols):
- if matrix[y][x] == 'O':
- output += y * 100 + x
- return output
- print(sum_gps_coordinates(grid, moves))
- # part 2
- wide_grid = []
- moves = []
- with open("day15input.txt") as f:
- input_lines = f.read().splitlines()
- first_part = True
- for line in input_lines:
- if not line:
- first_part = False
- elif not first_part:
- moves.append(line)
- elif first_part:
- temp = []
- for char in line:
- if char == "O":
- temp.extend(["[", "]"])
- elif char == "#":
- temp.extend(["#"] * 2)
- elif char == ".":
- temp.extend(["."] * 2)
- elif char == "@":
- temp.extend(["@", "."])
- wide_grid.append(temp)
- moves = "".join(moves)
- def sum_gps_coordinates2(matrix: list[list[str]], instructions: str) -> int:
- def push(stack: list[list[tuple[int, int]]], direction: str):
- nonlocal matrix, step
- offset = step[direction]
- if direction == "<" or direction == ">":
- stack = stack[0]
- y3, x3 = stack[-1]
- while matrix[y3][x3] != '#' and matrix[y3][x3] != '.':
- x3 += offset[1]
- stack.append((y3, x3))
- if matrix[y3][x3] == '#':
- return
- else:
- curr_y, curr_x = y3, x3
- while stack:
- next_y, next_x = stack.pop()
- matrix[curr_y][curr_x], matrix[next_y][next_x] = matrix[next_y][next_x], matrix[curr_y][curr_x]
- curr_y, curr_x = next_y, next_x
- return
- elif direction == '^' or direction == 'v':
- temp = {(1, 1)}
- while temp:
- temp = set()
- for coord in stack[-1]:
- if matrix[coord[0] + offset[0]][coord[1]] == '#':
- return
- if matrix[coord[0] + offset[0]][coord[1]] == '[':
- temp.add((coord[0] + offset[0], coord[1]))
- temp.add((coord[0] + offset[0], coord[1] + 1))
- elif matrix[coord[0] + offset[0]][coord[1]] == ']':
- temp.add((coord[0] + offset[0], coord[1]))
- temp.add((coord[0] + offset[0], coord[1] - 1))
- stack.append(list(temp))
- stack.pop()
- while stack:
- curr_tiles = stack.pop()
- for tile in curr_tiles:
- (matrix[tile[0]][tile[1]],
- matrix[tile[0]+offset[0]][tile[1]]) = (matrix[tile[0]+offset[0]][tile[1]],
- matrix[tile[0]][tile[1]])
- return
- step = {'<': (0, -1), 'v': (1, 0), '>': (0, 1), '^': (-1, 0)}
- rows = len(matrix)
- cols = len(matrix[0])
- # find starting position
- curr_pos = (-1, -1)
- for y in range(rows):
- for x in range(cols):
- if matrix[y][x] == '@':
- curr_pos = (y, x)
- break
- if curr_pos != (-1, -1):
- break
- # execute instructions
- for i in instructions:
- y1, x1 = curr_pos
- dy, dx = step[i]
- y2, x2 = y1 + dy, x1 + dx # this is the adjacent tile in the current direction
- if matrix[y2][x2] == "[":
- push([[(y2, x2), (y2, x2+1)]], i)
- elif matrix[y2][x2] == "]":
- temp = []
- temp.append([(y2, x2), (y2, x2-1)])
- push([[(y2, x2), (y2, x2-1)]], i)
- if matrix[y2][x2] == '.':
- matrix[y1][x1], matrix[y2][x2] = matrix[y2][x2], matrix[y1][x1]
- curr_pos = y2, x2
- # after all moves, calculate output
- output = 0
- for y in range(rows):
- for x in range(cols):
- if matrix[y][x] == '[':
- output += y * 100 + x
- return output
- print(sum_gps_coordinates2(wide_grid, moves))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement