Beispiel #1
0
 def scan(self, pos):
     qin = Queue()
     qout = Queue()
     qin.put(pos[0])
     qin.put(pos[1])
     vm = IntCodeVM(self.memory, qin, qout)
     vm.run()
     return qout.get()
Beispiel #2
0
class Robot:
    LEFT = 0
    RIGHT = 1

    DIRECTIONS = {
        'up': (0, 1),
        'right': (1, 0),
        'down': (0, -1),
        'left': (-1, 0)
    }

    def __init__(self, program, panel):
        self.vm = IntCodeVM(program, [])
        self.position = (0, 0)
        self.direction = self.DIRECTIONS['up']
        self.panel = panel

    def run(self):
        while True:
            if self.vm.finished():
                return

            self.vm.add_input(self.panel.color(self.position))

            color = next(self.vm.run())
            direction = next(self.vm.run())

            self.panel.paint(self.position, color)
            self.move(direction)

    def move(self, direction):
        if direction == self.LEFT:
            if self.direction == self.DIRECTIONS['up']:
                self.direction = self.DIRECTIONS['left']
            elif self.direction == self.DIRECTIONS['left']:
                self.direction = self.DIRECTIONS['down']
            elif self.direction == self.DIRECTIONS['down']:
                self.direction = self.DIRECTIONS['right']
            elif self.direction == self.DIRECTIONS['right']:
                self.direction = self.DIRECTIONS['up']
        if direction == self.RIGHT:
            if self.direction == self.DIRECTIONS['up']:
                self.direction = self.DIRECTIONS['right']
            elif self.direction == self.DIRECTIONS['right']:
                self.direction = self.DIRECTIONS['down']
            elif self.direction == self.DIRECTIONS['down']:
                self.direction = self.DIRECTIONS['left']
            elif self.direction == self.DIRECTIONS['left']:
                self.direction = self.DIRECTIONS['up']

        px, py = self.position
        dx, dy = self.direction
        self.position = (px + dx, py + dy)
Beispiel #3
0
class Robot:
    def __init__(self, memory):
        self.brain = IntCodeVM(memory, self, self)
        self.white = set()
        self.black = set()
        self.location = (0, 0)
        self.direction = (0, -1)
        self.put_state = 0

    def get(self):
        "What colour is the current location?"
        if self.location in self.white:
            return 1
        return 0

    def put(self, value):
        "Take input from the brain"
        if self.put_state == 0:
            # What to paint
            if value == 0:
                if self.location in self.white:
                    self.white.remove(self.location)
                self.black.add(self.location)
            elif value == 1:
                if self.location in self.black:
                    self.black.remove(self.location)
                self.white.add(self.location)
            else:
                raise Exception("unexpected paint instruction")
            self.put_state = 1
        elif self.put_state == 1:
            # How to turn
            if value == 0:
                # turn left
                self.direction = (-self.direction[1], self.direction[0])
            elif value == 1:
                # turn right
                self.direction = (self.direction[1], -self.direction[0])
            else:
                raise Exception("unexpected turn instruction")
            self.put_state = 0
            self.location = (
                self.location[0] + self.direction[0],
                self.location[1] + self.direction[1],
            )
            print(f"Now at {self.location}")
        else:
            raise Exception("state machine fail")

    def run(self):
        "Go!"
        self.brain.run()
Beispiel #4
0
        self.grid = Grid()

    def put(self, value):
        if value == 10:
            self.pos = (0, self.pos[1] + 1)
            return
        self.grid[self.pos] = chr(value)
        self.pos = (self.pos[0] + 1, self.pos[1])


infile = open(sys.argv[1])
memory = read_memory(infile)
reader = GridReader()
vm = IntCodeVM(memory, reader, reader)

vm.run()

reader.grid.print()


def adjacent(pos):
    return [
        (pos[0] + 1, pos[1]),
        (pos[0] - 1, pos[1]),
        (pos[0], pos[1] + 1),
        (pos[0], pos[1] - 1),
    ]


def find_intersections(grid):
    scaf = [pos for pos in grid.places if grid[pos] == "#"]
Beispiel #5
0
class Arcade:
    BLOCK_TILE = 2
    PADDLE_TILE = 3
    BALL_TILE = 4

    JOYSTICK_NEUTRAL = 0
    JOYSTICK_LEFT = -1
    JOYSTICK_RIGHT = 1

    def __init__(self, program, play_for_free=False, debug=False):
        if play_for_free:
            program = self._remove_check_for_quarters(program)
        self.debug = debug
        self.vm = IntCodeVM(program)
        self.grid = Grid()

        self.joystick_direction = self.JOYSTICK_NEUTRAL
        self.score = None

    def run_instruction(self):
        x, y, value = self.get_instruction()
        if self.is_score(x, y):
            self.score = value
            self._debug(f"Setting score to {value}")
        else:
            self._debug(f'Setting tile of type {value} to ({x}, {y})')
            self.set_tile(x, y, value)
            self.play()

    def play(self):
        self._debug(
            f'Paddle: {self.paddle_position()}, Ball: {self.ball_position()}, Direction: {self.joystick_direction}'
        )

        if None in [self.paddle_position(), self.ball_position()]:
            return
        if self.paddle_position()[0] > self.ball_position()[0]:
            self.move_joystick(self.JOYSTICK_LEFT)
        elif self.paddle_position()[0] < self.ball_position()[0]:
            self.move_joystick(self.JOYSTICK_RIGHT)
        else:
            self.move_joystick(self.JOYSTICK_NEUTRAL)

    def is_score(self, x, y):
        return x == -1 and y == 0

    def set_tile(self, x, y, tile):
        self.grid.set(x, y, tile)

    def get_instruction(self):
        return list(next(self.vm.run()) for _ in range(3))

    def run(self):
        while not self.vm.finished():
            self.vm.set_input(self.joystick_direction)
            self.run_instruction()

    def blocks_count(self):
        return self.grid.count_of_tiles_of_type(self.BLOCK_TILE)

    def ball_position(self):
        return self.grid.get_position_of(self.BALL_TILE)

    def paddle_position(self):
        return self.grid.get_position_of(self.PADDLE_TILE)

    def move_joystick(self, direction):
        assert (direction in [
            self.JOYSTICK_LEFT, self.JOYSTICK_NEUTRAL, self.JOYSTICK_RIGHT
        ])

        self.joystick_direction = direction

    def _remove_check_for_quarters(self, program):
        program[0] = 2
        return program

    def _debug(self, message):
        if self.debug:
            print('[DEBUG] ' + message)