def test_point(intcode, x, y, grid=None): computer = Computer(intcode, Queue(), Queue()) computer.start() in_q = computer.InputQueue() out_q = computer.OutputQueue() in_q.put(x) in_q.put(y) pull = out_q.get() computer.join() if pull: if grid: grid.set(Point(x, y), '#') return pull
class Robot: def __init__(self, intcode, columns, rows): self.computer = Computer(intcode, self, self) # Starts facing up. self.direction = Point(0, -1) self.current = Point(columns / 2, rows / 2) self.panels = Grid(rows, columns) # Set the starting position to white. (b) self.panels.set(self.current, '#') self.toggle = False self.painted = {} # map points to number of times they were painted. def turn(self, direction): # Turn left (0, -1) => (-1, 0) => (0, 1) => (1, 0) if direction == 0: self.direction = Point(self.direction.y, -self.direction.x) # Turn right (0, -1) => (1, 0) => (0, 1) => (-1, 0) if direction == 1: self.direction = Point(-self.direction.y, self.direction.x) # Move one step in the new direction self.current = Point(self.current.x + self.direction.x, self.current.y + self.direction.y) def paint(self, color): if not self.painted.has_key(self.current): self.painted[self.current] = 0 self.painted[self.current] += 1 value = '.' if color == 0 else '#' self.panels.set(self.current, value) def get(self): return 0 if self.panels.get(self.current) == '.' else 1 def put(self, value): if self.toggle: self.turn(value) else: self.paint(value) self.toggle = not self.toggle def run(self): output = self.computer.OutputQueue() self.computer.start() self.computer.join() def unique_painted(self): return len(self.painted.keys())
def main(): if (len(sys.argv) < 2): print 'Missing data file!' print 'Usage: python [script] [data]' sys.exit(1) f = open(sys.argv[1]) intcode = [int(x) for x in f.read().split(',')] f.close() computer = Computer(intcode, Queue(), Queue()) computer.daemon = True _ = system('clear') computer.start() display_prompt(computer.OutputQueue()) if sys.argv[2] == 'a': walk_hull(computer) else: run_hull(computer)