Advertisement
Guest User

D15New

a guest
Dec 15th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. from aoc.intcomp import IntComp
  2.  
  3. computer = IntComp("day15.txt")
  4. command = 1
  5. x, y = (0, 0)
  6. valid_squares = []
  7.  
  8.  
  9. def try_squares():
  10.     for i in range(1, 5):
  11.         computer.run_program(i)
  12.         status = computer.get_output()[0]
  13.         computer.clear_output()
  14.  
  15.         if status == 0:
  16.             # Blocked
  17.             pass
  18.         elif status == 1:
  19.             # Move successful
  20.             update_coords(i)
  21.             valid_squares.append((x, y))
  22.             return_to_last(i)
  23.         elif status == 2:
  24.             # Goal found
  25.             print('Found the goal')
  26.             break
  27.  
  28.  
  29. def update_coords(direction):
  30.     global x, y
  31.     if direction == 1:
  32.         y += 1  # North
  33.     elif direction == 2:
  34.         y -= 1  # South
  35.     elif direction == 3:
  36.         x -= 1  # West
  37.     elif direction == 4:
  38.         x += 1  # East
  39.  
  40.  
  41. def return_to_last(direction_moved):
  42.     opposite = {1: 2, 2: 1, 3: 4, 4: 3}
  43.     computer.run_program(opposite[direction_moved])
  44.     update_coords(opposite[direction_moved])
  45.  
  46.  
  47. try_squares()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement