Beispiel #1
0
class Nic(object):

    def __init__(self, address: int, program: List[int]):
        self.inputQueue = [address]
        self._computer = IntCodeComputer(program)
        self._computer.inputsProvider = lambda: self.inputQueue.pop(0) if self.inputQueue else -1

    def execute(self, send: Callable[[int, int, int], None]):
        send(self._computer.execute(),
             self._computer.execute(),
             self._computer.execute())
Beispiel #2
0
class HullPaintingRobot(object):
    def __init__(self, program: List[int], defaultPanelColor=Color.black):
        self.computer = IntCodeComputer(program)
        self.direction = Direction()
        self.panelColorByXy = defaultdict(lambda: defaultPanelColor)
        (self.x, self.y) = (0, 0)
        # don't access dict for the initial panel color, as it would create an entry for a panel that was not colored
        oldPanelColor = defaultPanelColor
        while not self.computer.halted:
            newPanelColor = self.computer.execute([oldPanelColor])
            oldPanelColor = self.panelColorByXy[(self.x, self.y)]
            self.panelColorByXy[(self.x, self.y)] = newPanelColor
            turn = self.computer.execute([])
            self.x, self.y = self.direction.move(self.x, self.y, turn)