Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- field = {}
- field[(0, 0)] = 1
- x = 0
- y = 0
- # Start in an upward facing position (index 0)
- # Turning left will move the index left, turning right will move the index right (wrapping around in both cases)
- dir_vectors = [(0, 1), (1, 0), (0, -1), (-1, 0)]
- dir_idx = 0
- class IntcodeComputerFieldInput:
- def read(self):
- color = field.get((x, y))
- if color is None:
- return 0
- else:
- return color
- class IntcodeComputerFieldOutput:
- def __init__(self):
- # Store partial instruction
- self.color = None
- def write(self, output):
- if self.color is None:
- self.color = output
- else:
- global x, y, dir_idx
- # Paint the square
- field[(x, y)] = self.color
- # Turn and move
- # 0 is left, 1 is right
- if output == 0:
- dir_idx = (dir_idx - 1) % 4
- elif output == 1:
- dir_idx = (dir_idx + 1) % 4
- else:
- raise Exception("Unknown direction code: {}".format(output))
- x += dir_vectors[dir_idx][0]
- y += dir_vectors[dir_idx][1]
- # That finishes the two part instruction
- self.color = None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement