Exemple #1
0
def part2():
    program = get_input()
    program[0] = 2
    vm, screen, score = IntcodeVm(program, []), {}, 0
    paddle_x, ball_x = 0, 0

    def update():
        nonlocal score, paddle_x, ball_x
        while True:
            a, b, c = vm.receive(), vm.receive(), vm.receive()
            if a is None or b is None or c is None:
                break
            if (a, b) != (-1, 0):
                screen[(a, b)] = c
                if c == 3: paddle_x = a
                if c == 4: ball_x = a
            else: score = c

    def joystick_pos():
        if paddle_x < ball_x: return 1
        elif paddle_x > ball_x: return -1
        else: return 0

    while True:
        try:
            update()
            vm.send(joystick_pos())
        except StopIteration:
            break

    return score
Exemple #2
0
def run_springscript(instructions):
    vm = IntcodeVm(MAIN_PROGRAM)
    for instr in instructions:
        for c in instr:
            vm.send(ord(c))
        vm.send(ord('\n'))

    outputs = list(vm.outputs)
    if outputs[-1] > 255:
        print(outputs[-1])
    else:
        print(''.join(chr(c) for c in outputs))
Exemple #3
0
def play_game():
    vm = IntcodeVm(MAIN_PROGRAM)
    while True:
        while True:
            try:
                char = next(vm.outputs)
                if char is None: break
                print(chr(char), end='')
            except StopIteration:
                return
        command = input('Enter command: ')
        for char in command + '\n':
            vm.send(ord(char))
Exemple #4
0
def paint(prog, initial_white_tiles = []):
    vm = IntcodeVm(prog)
    visited, whites, pos, direction = set(), set(initial_white_tiles), (0, 0), 'U'

    while True:
        vm.send(1 if pos in whites else 0)
        try:
            paint, lr = vm.receive(), vm.receive()
            visited.add(pos)
            if paint == 1:
                whites.add(pos)
            elif paint == 0:
                whites.discard(pos)
            direction = turn(direction, lr)
            pos = move(pos, direction)
        except StopIteration:
            break

    return visited, whites
Exemple #5
0
def is_beam(x, y):
    vm = IntcodeVm(PROGRAM)
    vm.send(x)
    vm.send(y)
    return vm.receive() == 1