Guest User

Untitled

a guest
Dec 15th, 2024
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. # Input: grid of # O . @ (border all #)
  2. #        then blank line, then sequence of < ^ > v characters
  3. #        but this time, translate
  4. #          # -> ##
  5. #          O -> []
  6. #          . -> ..
  7. #          @ -> @.
  8. # Output: @ attempts to make those moves, pushing boxes ([]),
  9. #         unless they would hit a wall (#) in case nothing moves
  10. #         then sum of (100 * row + column) over left side of all boxes
  11.  
  12. robot_r, robot_c = -1, -1
  13.  
  14. def would_hit_wall(cr, cc):
  15.   for wall in walls:
  16.     if cr == wall[0] and cc == wall[1]:
  17.       return True
  18.   return False
  19.  
  20. def try_to_move():
  21.   global robot_r
  22.   global robot_c
  23.   new_r, new_c = robot_r + dr, robot_c + dc
  24.   if would_hit_wall(new_r, new_c):
  25.     return
  26.   box_in_way = None
  27.   for box in boxes:
  28.     if new_r == box[0] and new_c - box[1] in [0, 1]:
  29.       box_in_way = box
  30.       break
  31.   if box_in_way == None:
  32.     robot_r, robot_c = new_r, new_c
  33.     return
  34.   boxes_can_move = True
  35.   boxes_to_examine = [box]
  36.   boxes_to_move = []
  37.   while len(boxes_to_examine) > 0:
  38.     box = boxes_to_examine[0]
  39.     boxes_to_examine = boxes_to_examine[1:]
  40.     if box in boxes_to_move:
  41.       continue
  42.     if would_hit_wall(box[0] + dr, box[1] + dc) or would_hit_wall(box[0] + dr, box[1] + dc + 1):
  43.       boxes_can_move = False
  44.       break
  45.     boxes_to_move.append(box)
  46.     for other_box in boxes:
  47.       if other_box in boxes_to_move:
  48.         continue
  49.       if box[0] + dr == other_box[0] and box[1] + dc - other_box[1] in [-1, 0, 1]:
  50.         boxes_to_examine.append(other_box)
  51.   if boxes_can_move:
  52.     robot_r, robot_c = new_r, new_c
  53.     for box in boxes_to_move:
  54.       box[0] += dr
  55.       box[1] += dc
  56.   return
  57.  
  58. walls = []
  59. boxes = []
  60. directions = ""
  61.  
  62. found_blank_line = False
  63. r = 0
  64. file = open("15_input.txt", "r")
  65. for line in file:
  66.   line = line.replace("\n", "")
  67.   if line == "":
  68.     found_blank_line = True
  69.     continue
  70.   if found_blank_line:
  71.     directions += line
  72.     continue
  73.   c = 0
  74.   for character in line:
  75.     if character == "@":
  76.       robot_r, robot_c = r, c
  77.     if character == "#":
  78.       walls.append([r, c])
  79.       walls.append([r, c + 1])
  80.     if character == "O":
  81.       boxes.append([r, c])
  82.     c += 2
  83.   r += 1
  84.  
  85. for character in directions:
  86.   dr, dc = -2, -2
  87.   if character == "^":
  88.     dr, dc = -1, 0
  89.   if character == "v":
  90.     dr, dc = 1, 0
  91.   if character == "<":
  92.     dr, dc = 0, -1
  93.   if character == ">":
  94.     dr, dc = 0, 1
  95.   if dr == -2:
  96.     continue
  97.   try_to_move()
  98.  
  99. total = 0
  100. for box in boxes:
  101.   total += 100 * box[0] + box[1]
  102. print (total)
Add Comment
Please, Sign In to add comment