Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1.  
  2. field = {}
  3. field[(0, 0)] = 1
  4.  
  5. x = 0
  6. y = 0
  7.  
  8. # Start in an upward facing position (index 0)
  9. # Turning left will move the index left, turning right will move the index right (wrapping around in both cases)
  10. dir_vectors = [(0, 1), (1, 0), (0, -1), (-1, 0)]
  11. dir_idx = 0
  12.  
  13. class IntcodeComputerFieldInput:
  14.  
  15.     def read(self):
  16.         color = field.get((x, y))
  17.         if color is None:
  18.             return 0
  19.         else:
  20.             return color
  21.  
  22.  
  23. class IntcodeComputerFieldOutput:
  24.  
  25.     def __init__(self):
  26.         # Store partial instruction
  27.         self.color = None
  28.  
  29.     def write(self, output):
  30.         if self.color is None:
  31.             self.color = output
  32.         else:
  33.             global x, y, dir_idx
  34.  
  35.             # Paint the square
  36.             field[(x, y)] = self.color
  37.  
  38.             # Turn and move
  39.             # 0 is left, 1 is right
  40.             if output == 0:
  41.                 dir_idx = (dir_idx - 1) % 4
  42.             elif output == 1:
  43.                 dir_idx = (dir_idx + 1) % 4
  44.             else:
  45.                 raise Exception("Unknown direction code: {}".format(output))
  46.  
  47.             x += dir_vectors[dir_idx][0]
  48.             y += dir_vectors[dir_idx][1]
  49.  
  50.             # That finishes the two part instruction
  51.             self.color = None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement