Exemplo n.º 1
0
    def test_jump_immediate(self):
        ic = IntCode("3,3,1105,-1,9,1101,0,0,12,4,12,99,1")
        ic.add_input(0)
        ic.run()
        self.assertEqual(0, ic.output[0])

        ic.add_input(1)
        ic.reset()
        ic.run()
        self.assertEqual(1, ic.output[0])
Exemplo n.º 2
0
    def test_jump_position(self):
        ic = IntCode("3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9")
        ic.add_input(0)
        ic.run()
        self.assertEqual(0, ic.output[0])

        ic.add_input(1)
        ic.reset()
        ic.run()
        self.assertEqual(1, ic.output[0])
Exemplo n.º 3
0
    def test_less_than_8_immediate(self):
        ic = IntCode("3,3,1107,-1,8,3,4,3,99")
        ic.add_input(7)
        ic.run()
        self.assertEqual(1, ic.output[0])

        ic.add_input(8)
        ic.reset()
        ic.run()
        self.assertEqual(0, ic.output[0])
Exemplo n.º 4
0
    def test_less_than_8_position(self):
        ic = IntCode("3,9,7,9,10,9,4,9,99,-1,8")
        ic.add_input(7)
        ic.run()
        self.assertEqual(1, ic.output[0])

        ic.add_input(8)
        ic.reset()
        ic.run()
        self.assertEqual(0, ic.output[0])
Exemplo n.º 5
0
def part2(ic: IntCode):
    for i in range(0, 100):
        for j in range(0, 100):
            ic.reset()
            ic[1] = i
            ic[2] = j
            ic.run()
            if ic[0] == 19690720:
                print(f"{i}, {j} = {ic[0]}")
                assert i == 89 and j == 76
                break
Exemplo n.º 6
0
def main():
    data = get_data()
    program = data.copy()
    program[1] = 12
    program[2] = 2
    computer = IntCode(program)
    computer.run()
    print('Part 1: {}'.format(computer.program[0]))
    for noun in range(1,100):
        for verb in range(1,100):
            computer.reset(data.copy())
            computer.program[1] = noun
            computer.program[2] = verb
            computer.run()
            if computer.program[0] == 19690720:
                print('Part 2: Noun*100 + Verb = {}'.format(100 * noun + verb))
                return
Exemplo n.º 7
0
    def test_equality(self):
        ic = IntCode("""
3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,
1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,
999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99""")
        ic.add_input(7)
        ic.run()
        self.assertEqual(999, ic.output[0])

        ic.add_input(8)
        ic.reset()
        ic.run()
        self.assertEqual(1000, ic.output[0])

        ic.add_input(9)
        ic.reset()
        ic.run()
        self.assertEqual(1001, ic.output[0])
Exemplo n.º 8
0
def part2(ic: IntCode):
    ic.add_input(5)
    ic.reset()
    ic.run()
    print(ic.output[0])
    assert ic.output[0] == 513116
Exemplo n.º 9
0
def part2(ic: IntCode):
    ic.add_input(2)
    ic.reset()
    ic.run()
    assert ic.output[0] == 83089
Exemplo n.º 10
0
class ArcadeCabinet:
    def __init__(self, program: str) -> None:
        self._program = IntCode(program, self._read_joystick_input)
        self._screen_buffer: Dict[Tuple[int, int],
                                  Tile] = defaultdict(lambda: Tile.EMPTY)
        self._score = 0
        self._screen = None

    def reset(self) -> None:
        self._program.reset()

    @staticmethod
    def _init_colours():
        curses.start_color()
        curses.use_default_colors()

        curses.init_pair(Tile.EMPTY, curses.COLOR_BLACK, curses.COLOR_BLACK)
        curses.init_pair(Tile.WALL, curses.COLOR_WHITE, curses.COLOR_BLACK)
        curses.init_pair(Tile.BLOCK, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
        curses.init_pair(Tile.PADDLE, curses.COLOR_GREEN, curses.COLOR_BLACK)
        curses.init_pair(Tile.BALL, curses.COLOR_RED, curses.COLOR_BLACK)

    @staticmethod
    def _create_window():
        width = 36 + 1
        height = 19 + 2
        return curses.newwin(height, width, 0, 0)

    def run(self) -> None:
        curses.wrapper(self._run)

    def _run(self, stdscr) -> None:
        self._screen = self._create_window()
        self._screen.nodelay(True)
        self._init_colours()

        while not self._program.halted:
            self._program.run(pause_on_output=True)
            x = self._program.output[0]

            self._program.run(pause_on_output=True)
            y = self._program.output[0]

            self._program.run(pause_on_output=True)
            output = self._program.output[0]

            if x == -1 and y == 0:
                self._score = output
                self._screen.addstr(20, 1, f"SCORE: {output}")
            else:
                tile = Tile(output)
                self._screen_buffer[x, y] = tile

                self._screen.addch(
                    y,
                    x,
                    self._get_char_for_tile(tile, x, y),
                    curses.color_pair(tile),
                )
            self._screen.move(20, 36)
            self._screen.refresh()

    @property
    def dimensions(self) -> Tuple[int, int]:
        return 36, 19

    def get_number_of_tiles(self, tile_type: Tile) -> int:
        return len([t for t in self._screen_buffer.values() if t == tile_type])

    def insert_quarter(self, count: int = 1):
        self._program[0] = count

    def _get_char_for_tile(self, tile: Tile, x: int, y: int) -> str:
        width, height = self.dimensions
        if tile is Tile.EMPTY:
            return " "
        if tile is Tile.BLOCK:
            return "◼"
        if tile is Tile.BALL:
            return "●"
        if tile is Tile.PADDLE:
            return "▬"
        if tile is Tile.WALL:
            return self._get_wall_char(x, y, width, height)

    def _get_wall_char(self, x: int, y: int, width: int, height: int) -> str:
        if y == 0:
            if x == 0:
                return "┏"
            if x == width:
                return "┓"
            return "━"
        if y == height:
            if x == 0:
                return "┗"
            if x == width:
                return "┛"
            return "━"
        return "┃"

    def _read_joystick_input(self) -> int:
        delay = 0.4
        start = time.monotonic()
        key = None
        while (taken := time.monotonic() - start) < delay:
            key = self._screen.getch()

            if key in (curses.KEY_LEFT, curses.KEY_RIGHT, ord("r"), ord("a"),
                       ord("d")):
                break
        if key == ord("r"):
            self.reset()

        if taken < delay:
            time.sleep(delay - taken)

        if key == curses.KEY_LEFT or key == ord("a"):
            return Joystick.LEFT
        if key == curses.KEY_RIGHT or key == ord("d"):
            return Joystick.RIGHT
        return Joystick.NEUTRAL