Esempio n. 1
0
def part2(path):
    program = read_program(path)
    program[0] = 2
    c = Computer(program)
    score = None
    paddle = None
    ball = None
    while c.ram[c.ip] != 99:
        # when prompted for controller input, have the paddle move toward the
        # ball, if their x coordinates are different
        if c.ram[c.ip] % 100 == 3:
            if paddle is None or ball is None:
                c.in_.append(0)
            elif paddle < ball:
                c.in_.append(1)
            elif paddle > ball:
                c.in_.append(-1)
            else:
                c.in_.append(0)
        c.step()
        if len(c.out) == 3:
            x, y, tile_id = (c.out.popleft() for _ in range(3))
            if (x, y) == (-1, 0):
                score = tile_id
            elif tile_id == 3:
                paddle = x
            elif tile_id == 4:
                ball = x
    return score
Esempio n. 2
0
def get_drone_status(ram, x, y):
    '''Return the status of the drone at coordinates (x,y), calculated by an
    Intcode computer using the supplied program.'''
    comp = Computer(ram)
    comp.in_.append(x)
    comp.in_.append(y)
    while not comp.out:
        comp.step()
    return comp.out.popleft()
Esempio n. 3
0
def part1(path):
    c = Computer(read_program(path))
    tiles = {id: set() for id in range(5)}
    while c.ram[c.ip] != 99:
        c.step()
        if len(c.out) == 3:
            x, y, tile_id = (c.out.popleft() for _ in range(3))
            tiles[tile_id].add((x, y))
    return len(tiles[2])
Esempio n. 4
0
def partX(path, in_, debug=False):
    program = read_program(path)
    c = Computer(program)
    c.in_.append(in_)
    while c.ram[c.ip] != 99:
        c.step(debug)
    if len(c.out) != 1:
        print(f'WARNING: expected 1 output, received {len(c.out)} instead')
        print(f'c.out == {c.out}')
    return c.out[-1]
Esempio n. 5
0
def part2(progpath, routpath):
    ram = get_program(progpath)
    ram[0] = 2
    c = Computer(ram)
    with open(routpath, 'r') as fobj:
        routine = fobj.read()
    for char in routine:
        c.in_.append(ord(char))
    #disable video feed
    c.in_.extend((ord('n'), ord('\n')))
    while c.ram[c.ip] != 99:
        c.step()
    return c.out.pop()
Esempio n. 6
0
def run_program(path, start_white=False):
    '''Returns the Robot that runs the program loaded from path.'''
    program = read_program(path)
    c = Computer(program)
    r = Robot()
    paint_next = True

    if start_white:
        r.painted[(0, 0)] = 1

    while c.ram[c.ip] != 99:
        if c.ram[c.ip] == 3:  #waiting for input
            c.in_.append(r.get_color())
        c.step()
        if c.out:
            out = c.out.popleft()
            if paint_next:
                r.paint(out)
            else:
                r.move(out)
            paint_next = not paint_next
    return r
Esempio n. 7
0
def get_scaffold(ram):
    '''Run the Intcode program to get the scaffold output.'''
    c = Computer(ram)
    while c.ram[c.ip] != 99:
        c.step()
    return ''.join([chr(s) for s in c.out]).strip().split('\n')