question: --- Day 15: Warehouse Woes ---
  
  You appear back inside your own mini submarine! Each Historian drives their mini submarine in a different direction; maybe the Chief has his own submarine down here somewhere as well?
  
  You look up to see a vast school of lanternfish swimming past you. On closer inspection, they seem quite anxious, so you drive your mini submarine over to see if you can help.
  
  Because lanternfish populations grow rapidly, they need a lot of food, and that food needs to be stored somewhere. That's why these lanternfish have built elaborate warehouse complexes operated by robots!
  
  These lanternfish seem so anxious because they have lost control of the robot that operates one of their most important warehouses! It is currently running amok, pushing around boxes in the warehouse with no regard for lanternfish logistics or lanternfish inventory management strategies.
  
  Right now, none of the lanternfish are brave enough to swim up to an unpredictable robot so they could shut it off. However, if you could anticipate the robot's movements, maybe they could find a safe option.
  
  The lanternfish already have a map of the warehouse and a list of movements the robot will attempt to make (your puzzle input). The problem is that the movements will sometimes fail as boxes are shifted around, making the actual movements of the robot difficult to predict.
  
  For example:
  
  ##########
  #..O..O.O#
  #......O.#
  #.OO..O.O#
  #..O@..O.#
  #O#..O...#
  #O..O..O.#
  #.OO.O.OO#
  #....O...#
  ##########
  
  <vv>^<v^>v>^vv^v>v<>v^v<v<^vv<<<^><<><>>v<vvv<>^v^>^<<<><<v<<<v^vv^v>^
  vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<<v<^v>^<^^>>>^<v<v
  ><>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^<v>v^^<^^vv<
  <<v<^>>^^^^>>>v^<>vvv^><v<<<>^^^vv^<vvv>^>v<^^^^v<>^>vvvv><>>v^<<^^^^^
  ^><^><>>><>^^<<^^v>>><^<v>^<vv>>v>>>^v><>^v><<<<v>>v<v<v>vvv>^<><<>^><
  ^>><>^v<><^vvv<^^<><v<<<<<><^v<<<><<<^^<v<^^^><^>>^<v^><<<^>>^v<v^v<v^
  >^>>^v>vv>^<<^v<>><<><<v<<v><>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^
  <><^^>^^^<><vvvvv^v<v<<>^v<v>v<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<>
  ^^>vv<^v^v<vv>^<><v<^v>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<><<v>
  v^^>>><<^^<>>^v^<v^vv<>v^<<>^<^v^v><^<<<><<^<v><v<>vv>>v><v^<vv<>v^<<^
  As the robot (@) attempts to move, if there are any boxes (O) in the way, the robot will also attempt to push those boxes. However, if this action would cause the robot or a box to move into a wall (#), nothing moves instead, including the robot. The initial positions of these are shown on the map at the top of the document the lanternfish gave you.
  
  The rest of the document describes the moves (^ for up, v for down, < for left, > for right) that the robot will attempt to make, in order. (The moves form a single giant sequence; they are broken into multiple lines just to make copy-pasting easier. Newlines within the move sequence should be ignored.)
  
  Here is a smaller example to get started:
  
  ########
  #..O.O.#
  ##@.O..#
  #...O..#
  #.#.O..#
  #...O..#
  #......#
  ########
  
  <^^>>>vv<v>>v<<
  Were the robot to attempt the given sequence of moves, it would push around the boxes as follows:
  
  Initial state:
  ########
  #..O.O.#
  ##@.O..#
  #...O..#
  #.#.O..#
  #...O..#
  #......#
  ########
  
  Move <:
  ########
  #..O.O.#
  ##@.O..#
  #...O..#
  #.#.O..#
  #...O..#
  #......#
  ########
  
  Move ^:
  ########
  #.@O.O.#
  ##..O..#
  #...O..#
  #.#.O..#
  #...O..#
  #......#
  ########
  
  Move ^:
  ########
  #.@O.O.#
  ##..O..#
  #...O..#
  #.#.O..#
  #...O..#
  #......#
  ########
  
  Move >:
  ########
  #..@OO.#
  ##..O..#
  #...O..#
  #.#.O..#
  #...O..#
  #......#
  ########
  
  Move >:
  ########
  #...@OO#
  ##..O..#
  #...O..#
  #.#.O..#
  #...O..#
  #......#
  ########
  
  Move >:
  ########
  #...@OO#
  ##..O..#
  #...O..#
  #.#.O..#
  #...O..#
  #......#
  ########
  
  Move v:
  ########
  #....OO#
  ##..@..#
  #...O..#
  #.#.O..#
  #...O..#
  #...O..#
  ########
  
  Move v:
  ########
  #....OO#
  ##..@..#
  #...O..#
  #.#.O..#
  #...O..#
  #...O..#
  ########
  
  Move <:
  ########
  #....OO#
  ##.@...#
  #...O..#
  #.#.O..#
  #...O..#
  #...O..#
  ########
  
  Move v:
  ########
  #....OO#
  ##.....#
  #..@O..#
  #.#.O..#
  #...O..#
  #...O..#
  ########
  
  Move >:
  ########
  #....OO#
  ##.....#
  #...@O.#
  #.#.O..#
  #...O..#
  #...O..#
  ########
  
  Move >:
  ########
  #....OO#
  ##.....#
  #....@O#
  #.#.O..#
  #...O..#
  #...O..#
  ########
  
  Move v:
  ########
  #....OO#
  ##.....#
  #.....O#
  #.#.O@.#
  #...O..#
  #...O..#
  ########
  
  Move <:
  ########
  #....OO#
  ##.....#
  #.....O#
  #.#O@..#
  #...O..#
  #...O..#
  ########
  
  Move <:
  ########
  #....OO#
  ##.....#
  #.....O#
  #.#O@..#
  #...O..#
  #...O..#
  ########
  The larger example has many more moves; after the robot has finished those moves, the warehouse would look like this:
  
  ##########
  #.O.O.OOO#
  #........#
  #OO......#
  #OO@.....#
  #O#.....O#
  #O.....OO#
  #O.....OO#
  #OO....OO#
  ##########
  The lanternfish use their own custom Goods Positioning System (GPS for short) to track the locations of the boxes. The GPS coordinate of a box is equal to 100 times its distance from the top edge of the map plus its distance from the left edge of the map. (This process does not stop at wall tiles; measure all the way to the edges of the map.)
  
  So, the box shown below has a distance of 1 from the top edge of the map and 4 from the left edge of the map, resulting in a GPS coordinate of 100 * 1 + 4 = 104.
  
  #######
  #...O..
  #......
  The lanternfish would like to know the sum of all boxes' GPS coordinates after the robot finishes moving. In the larger example, the sum of all boxes' GPS coordinates is 10092. In the smaller example, the sum is 2028.
  
  Predict the motion of the robot and boxes in the warehouse. After the robot is finished moving, what is the sum of all boxes' GPS coordinates?
  
  Your puzzle answer was 1499739.
  
  --- Part Two ---
  
  The lanternfish use your information to find a safe moment to swim in and turn off the malfunctioning robot! Just as they start preparing a festival in your honor, reports start coming in that a second warehouse's robot is also malfunctioning.
  
  This warehouse's layout is surprisingly similar to the one you just helped. There is one key difference: everything except the robot is twice as wide! The robot's list of movements doesn't change.
  
  To get the wider warehouse's map, start with your original map and, for each tile, make the following changes:
  
  If the tile is #, the new map contains ## instead.
  If the tile is O, the new map contains [] instead.
  If the tile is ., the new map contains .. instead.
  If the tile is @, the new map contains @. instead.
  This will produce a new warehouse map which is twice as wide and with wide boxes that are represented by []. (The robot does not change size.)
  
  The larger example from before would now look like this:
  
  ####################
  ##....[]....[]..[]##
  ##............[]..##
  ##..[][]....[]..[]##
  ##....[]@.....[]..##
  ##[]##....[]......##
  ##[]....[]....[]..##
  ##..[][]..[]..[][]##
  ##........[]......##
  ####################
  Because boxes are now twice as wide but the robot is still the same size and speed, boxes can be aligned such that they directly push two other boxes at once. For example, consider this situation:
  
  #######
  #...#.#
  #.....#
  #..OO@#
  #..O..#
  #.....#
  #######
  
  <vv<<^^<<^^
  After appropriately resizing this map, the robot would push around these boxes as follows:
  
  Initial state:
  ##############
  ##......##..##
  ##..........##
  ##....[][]@.##
  ##....[]....##
  ##..........##
  ##############
  
  Move <:
  ##############
  ##......##..##
  ##..........##
  ##...[][]@..##
  ##....[]....##
  ##..........##
  ##############
  
  Move v:
  ##############
  ##......##..##
  ##..........##
  ##...[][]...##
  ##....[].@..##
  ##..........##
  ##############
  
  Move v:
  ##############
  ##......##..##
  ##..........##
  ##...[][]...##
  ##....[]....##
  ##.......@..##
  ##############
  
  Move <:
  ##############
  ##......##..##
  ##..........##
  ##...[][]...##
  ##....[]....##
  ##......@...##
  ##############
  
  Move <:
  ##############
  ##......##..##
  ##..........##
  ##...[][]...##
  ##....[]....##
  ##.....@....##
  ##############
  
  Move ^:
  ##############
  ##......##..##
  ##...[][]...##
  ##....[]....##
  ##.....@....##
  ##..........##
  ##############
  
  Move ^:
  ##############
  ##......##..##
  ##...[][]...##
  ##....[]....##
  ##.....@....##
  ##..........##
  ##############
  
  Move <:
  ##############
  ##......##..##
  ##...[][]...##
  ##....[]....##
  ##....@.....##
  ##..........##
  ##############
  
  Move <:
  ##############
  ##......##..##
  ##...[][]...##
  ##....[]....##
  ##...@......##
  ##..........##
  ##############
  
  Move ^:
  ##############
  ##......##..##
  ##...[][]...##
  ##...@[]....##
  ##..........##
  ##..........##
  ##############
  
  Move ^:
  ##############
  ##...[].##..##
  ##...@.[]...##
  ##....[]....##
  ##..........##
  ##..........##
  ##############
  This warehouse also uses GPS to locate the boxes. For these larger boxes, distances are measured from the edge of the map to the closest edge of the box in question. So, the box shown below has a distance of 1 from the top edge of the map and 5 from the left edge of the map, resulting in a GPS coordinate of 100 * 1 + 5 = 105.
  
  ##########
  ##...[]...
  ##........
  In the scaled-up version of the larger example from above, after the robot has finished all of its moves, the warehouse would look like this:
  
  ####################
  ##[].......[].[][]##
  ##[]...........[].##
  ##[]........[][][]##
  ##[]......[]....[]##
  ##..##......[]....##
  ##..[]............##
  ##..@......[].[][]##
  ##......[][]..[]..##
  ####################
  The sum of these boxes' GPS coordinates is 9021.
  
  Predict the motion of the robot and boxes in this new, scaled-up warehouse. What is the sum of all boxes' final GPS coordinates?
solution: INPUT_FILE = "input.txt"
  WALL = "#"
  ROBOT = "@"
  BOX = "O"
  
  BOX_LEFT = "["
  BOX_RIGHT = "]"
  
  MOVES = {"<": (-1, 0), ">": (1, 0), "^": (0, -1), "v": (0, 1)}
  
  
  class Warehouse:
      def __init__(self, width):
          self.width = width
          self.robot_location = (0, 0)
          self.walls = []
          self.boxes = {}
          self.moves = []
  
      def print(self):
          for y in range(self.height):
              for x in range(self.width):
                  p = (x, y)
                  if p == self.robot_location:
                      print(ROBOT, end="")
                  elif p in self.boxes:
                      print(self.boxes[p], end="")
                  elif p in self.walls:
                      print(WALL, end="")
                  else:
                      print(".", end="")
              print()
  
      def add_wall(self, wall):
          wall_position = (wall[0] * 2, wall[1])
          self.walls.append(wall_position)
          self.walls.append((wall_position[0] + 1, wall_position[1]))
  
      def add_box(self, box):
          box_position = (box[0] * 2, box[1])
          self.boxes[box_position] = BOX_LEFT
          self.boxes[(box_position[0] + 1, box_position[1])] = BOX_RIGHT
  
      def set_robot_location(self, location):
          self.robot_location = (location[0] * 2, location[1])
  
      def set_height(self, height):
          self.height = height
  
      def print_moves(self):
          for move in self.moves:
              print(move)
  
      def move(self, move):
          dx, dy = MOVES[move]
          new_location = (self.robot_location[0] + dx, self.robot_location[1] + dy)
          if new_location in self.walls:
              return
          if new_location in self.boxes:
              self.move_box(new_location, move)
          if new_location in self.boxes or new_location in self.walls:
              return
          self.robot_location = new_location
  
      def move_box(self, box, move):
          dx, dy = MOVES[move]
  
          if self.boxes[box] == BOX_RIGHT:
              box_left = (box[0] - 1, box[1])
              box_right = box
              new_box_left = (box_left[0] + dx, box_left[1] + dy)
              new_box_right = (box_right[0] + dx, box_right[1] + dy)
          else:
              box_left = box
              box_right = (box[0] + 1, box[1])
              new_box_left = (box_left[0] + dx, box_left[1] + dy)
              new_box_right = (box_right[0] + dx, box_right[1] + dy)
  
          # if horizontal
          if move == "<" or move == ">":
              if self.boxes[box] == BOX_RIGHT:
                  if new_box_left in self.walls or new_box_right in self.walls:
                      return False
                  if new_box_right != box_left:
                      if new_box_left in self.boxes or new_box_right in self.boxes:
                          if not self.move_box(new_box_left, move):
                              return False
                  if new_box_left in self.boxes:
                      if not self.move_box(new_box_left, move):
                          return False
                  self.boxes.pop(box_left)
                  self.boxes.pop(box_right)
                  self.boxes[new_box_left] = BOX_LEFT
                  self.boxes[new_box_right] = BOX_RIGHT
              elif self.boxes[box] == BOX_LEFT:
                  if new_box_left in self.walls or new_box_right in self.walls:
                      return False
                  if new_box_left != box_right:
                      if new_box_left in self.boxes or new_box_right in self.boxes:
                          if not self.move_box(new_box_right, move):
                              return False
                  if new_box_right in self.boxes:
                      if not self.move_box(new_box_right, move):
                          return False
                  self.boxes.pop(box_left)
                  self.boxes.pop(box_right)
                  self.boxes[new_box_left] = BOX_LEFT
                  self.boxes[new_box_right] = BOX_RIGHT
          else:
              if self.collision_check(box, move):
                  return False
              if new_box_left in self.walls or new_box_right in self.walls:
                  return False
              if new_box_left in self.boxes:
                  if not self.move_box(new_box_left, move):
                      return False
              if new_box_right in self.boxes:
                  if not self.move_box(new_box_right, move):
                      return False
              self.boxes.pop(box_left)
              self.boxes.pop(box_right)
              self.boxes[new_box_left] = BOX_LEFT
              self.boxes[new_box_right] = BOX_RIGHT
  
          return True
  
      def collision_check(self, box, move):
          collision = False
          dx, dy = MOVES[move]
  
          if self.boxes[box] == BOX_RIGHT:
              box_left = (box[0] - 1, box[1])
              box_right = box
              new_box_left = (box_left[0] + dx, box_left[1] + dy)
              new_box_right = (box_right[0] + dx, box_right[1] + dy)
          else:
              box_left = box
              box_right = (box[0] + 1, box[1])
              new_box_left = (box_left[0] + dx, box_left[1] + dy)
              new_box_right = (box_right[0] + dx, box_right[1] + dy)
          if new_box_left in self.walls or new_box_right in self.walls:
              return True
          if new_box_left in self.boxes:
              if self.collision_check(new_box_left, move):
                  return True
          if new_box_right in self.boxes:
              if self.collision_check(new_box_right, move):
                  return True
          return collision
  
      def run(self):
          for move in self.moves:
              self.move(move)
  
      def sum_of_box_locations(self):
          result = 0
          for x, y in self.boxes:
              if self.boxes[(x, y)] == BOX_LEFT:
                  result += x + 100 * y
          return result
  
  
  def read_file():
      with open(INPUT_FILE, "r") as f:
          lines = f.readlines()
          width = len(lines[0].strip()) * 2
          warehouse = Warehouse(width)
          moves = []
          for y, line in enumerate(lines):
              if line[0] == "#":
                  for x, c in enumerate(line.strip()):
                      p = (x, y)
                      if c == WALL:
                          warehouse.add_wall(p)
                      elif c == ROBOT:
                          warehouse.set_robot_location(p)
                      elif c == BOX:
                          warehouse.add_box(p)
              elif line[0] == "\n":
                  warehouse.set_height(y)
              else:
                  for move in line.strip():
                      moves.append(move)
  
              warehouse.moves = moves
  
          return warehouse
  
  
  warehouse = read_file()
  
  warehouse.print()
  warehouse.run()
  print(warehouse.sum_of_box_locations())
  