class Scanner(object): def __init__(self, size): self._board = Board(size, size, do_translate=False) self._program = load_program("dec19") self._x_given = False self._current_pos = None self._positions = [(x, y) for x in range(size) for y in range(size)] def _input(self): if self._current_pos is None: self._current_pos = self._positions.pop(0) if self._x_given: result = self._current_pos[1] self._x_given = False return result else: result = self._current_pos[0] self._x_given = True return result def _output(self, value): if value == 0: self._board.set(self._current_pos[0], self._current_pos[1], ".") elif value == 1: self._board.set(self._current_pos[0], self._current_pos[1], "#") else: raise ValueError("Invalid output value") self._current_pos = None def scan(self): while self._positions: intcode = IntCode(self._program) intcode.execute(self._input, self._output) self._board.print() return self._board.count("#")
class Arcade(object): def __init__(self, program): self._board = Board(100, 100, flip=True) self._machine = IntCode(program) self._acc = [] self._score = 0 self._direction = 0 self._ball_x = None self._paddle_x = None def play(self): self._machine.execute(input_func=self._joystick, output_func=self._draw) self._board.print() return self._count(), self._score def _joystick(self): if self._ball_x and self._paddle_x: self._direction = self._ball_x - self._paddle_x return self._direction def _draw(self, value): self._acc.append(value) if len(self._acc) == 3: x, y, tile_value = self._acc if x == -1 and y == 0: self._score = tile_value print("New score: {}".format(self._score)) self._board.print() print() else: tile = Tiles.from_value(tile_value) if tile == Tiles.BALL: self._ball_x = x elif tile == Tiles.PADDLE: self._paddle_x = x self._board.set(x, y, tile.char) self._acc = [] def _count(self): return self._board.count(Tiles.BLOCK.char)