def find_tank(prog, strategy):
    global position
    global grid
    ic = intcode.computer(prog)
    ic.set_input_handler(strategy)
    history = []
    while not ic.is_stopped():
        if loops > 100000:
            print("aborting after", loops, "loops")
            break

        resp = ic.run()
        if resp == None:
            continue

        new_pos = get_position(position, direction)

        if resp == 0:
            grid[new_pos] = 1
        elif resp == 1:
            grid[new_pos] = 2
            position = new_pos
            history.append(position)
        else:
            position = new_pos
            grid[new_pos] = 3
            render_grid()
            print("found tank at", position, "after", loops)
            return position
Beispiel #2
0
def compute(p, inp):
    cpu = intcode.computer(p)
    next(cpu)
    result = cpu.send(inp)
    while result == 0:
        result = next(cpu)
    return result
Beispiel #3
0
def calc_max_truster_signal(program, phaseSeq):
    data = 0
    for phase in phaseSeq:
        ic = intcode.computer(program.copy(), data)
        ic.set_phase(phase)
        data = ic.run()

    return data
Beispiel #4
0
def walk(p):
    p[0] = 2
    cpu = intcode.computer(p)
    inp = (ord(d) for d in ("A,A,B,C,A,C,B,C,A,B\n" "L,4,L,10,L,6\n"
        "L,6,L,4,R,8,R,8\n" "L,6,R,8,L,10,L,8,L,8\n" "n\n"))
    out = 0
    while out < 128:
        out = next(cpu)
        while out is None:
            out = cpu.send(next(inp))
    return out
Beispiel #5
0
def run_noun_verb_program(noun, verb):
    data = utilities.data(2)

    program = list(map(lambda x: int(x), data.split(',')))
    program[1] = noun
    program[2] = verb

    computer = intcode.computer(program)
    computer.run()

    return computer.memory[0]
Beispiel #6
0
def autoplay(p):
    cpu = intcode.computer(p)
    readline(cpu)
    commands = [
        "south", "south", "south", "south", "take festive hat", "north",
        "north", "north", "take whirled peas", "north", "north", "take coin",
        "north", "north", "west", "south", "west", "take mutex", "west",
        "south", "east"
    ]
    for c in commands:
        out = send(cpu, c + "\n")
    return re.search(r"typing (\d+) on", out).group(1)
Beispiel #7
0
def paint(p, initial):
    cpu = intcode.computer(p)
    next(cpu)
    panel = defaultdict(int)
    x, y, facing = 0, 0, 0
    moves = [(0,-1), (1,0), (0,1), (-1,0)]
    panel[x,y] = initial
    while True:
        panel[x,y] = cpu.send(panel[x,y])
        facing = (facing + 1 if next(cpu) else facing - 1) % len(moves)
        x += moves[facing][0]
        y += moves[facing][1]
        try: next(cpu)
        except StopIteration: return panel
Beispiel #8
0
def is_being_pulled(x,y):
    ic = intcode.computer(prog)

    pulled = 0
    ic.add_input(x)
    ic.add_input(y)
    while not ic.is_stopped():
        output = ic.run()
        if output == None:
            continue

        if output == 1:
            pulled+=1

    return pulled
Beispiel #9
0
def map_area(p, pos):
    cpu = intcode.computer(p)
    next(cpu)

    def traverse(pos):
        for i, move in enumerate(nesw(pos)):
            if move not in walls:
                walls[move] = cpu.send([1, 4, 2, 3][i])
                next(cpu)
                if walls[move]:
                    traverse(move)
                    cpu.send([2, 3, 1, 4][i])
                    next(cpu)  # backtrack

    traverse(pos)
Beispiel #10
0
def run_amps(p, phases, once):
    amps = []
    for phase in phases:
        amp = intcode.computer(p)  # or (p, 'cpu%d' % len(amps))
        next(amp)  # get to first yield (input)
        amp.send(int(phase))  # provide first input and continue
        amps.append(amp)

    out = 0
    ended = False
    while not ended:
        for amp in amps:
            out = amp.send(out)  # provide input, recieve output
            try:
                next(amp)  # continue until next yield for input
            except StopIteration:
                ended = True
        if once: ended = True
    return out
Beispiel #11
0
def emergency_hull_painting_robot(start_colour=0):
    # to record the postions that have been painted
    positions_painted = set()
    # to record the current colour of each squart
    grid = {}
    # starting direction
    direction = "^"
    pos = (0, 0)

    # 0 = black
    # 1 = white

    grid[(0, 0)] = start_colour

    ic = intcode.computer(intcode.get_program("11.input.txt"))

    while not ic.is_stopped():
        colour = 0  # default colour for a panel
        if pos in grid:  # lookup colour for panel if we have previously painted it
            colour = grid[pos]

        ic.add_input(colour)

        colour = ic.run()
        if colour == None:
            continue

        turn = ic.run()
        if turn == None:
            continue

        positions_painted.add(pos)
        grid[pos] = colour

        direction = change_direction(direction, turn)
        pos = move_position(pos, direction)

    return len(positions_painted), grid
Beispiel #12
0
def calc_max_truster_signal_feedback_loop(program, phaseSeq):
    amps = []
    for i in range(5):
        amp = intcode.computer(program)
        amp.set_phase(phaseSeq[i])
        amps.append(amp)

    data = 0
    i = 0
    amp_five_output = 0
    while True:
        amp = amps[i % 5]
        amp.add_input(data)
        data = amp.run()

        if i == 4:
            amp_five_output = data

        if amp.is_stopped():
            return amp_five_output

        i += 1
        if i > 4:
            i = 0
Beispiel #13
0
# More for the intcode computer
#
import unittest
import intcode


def get_program(filename):

    with open(filename) as fp:
        line = fp.readline()
        prog = []
        for d in line.strip().split(","):
            prog.append(int(d))
        return prog


if __name__ == '__main__':
    program = get_program('5.input.txt')

    ic = intcode.computer(program, 1)
    while not ic.is_stopped():
        output = ic.run()
        if output != None:
            print("part 1 output", output)

    ic = intcode.computer(program, 5)
    while not ic.is_stopped():
        output = ic.run()
        if output != None:
            print("part 2 output", output)
Beispiel #14
0
def get_program(filename):

    with open(filename) as fp:
        line = fp.readline()
        prog = []
        for d in line.strip().split(","):
            prog.append(int(d))
        return prog


if __name__ == '__main__':

    program = get_program('2.input.txt')
    program[1] = 12
    program[2] = 2
    ic = intcode.computer(program)
    ic.run()
    p = ic.get_program()

    print("part 1 output", p[0])

    running = True
    for a in range(100):
        for b in range(100):
            p = program
            p[1] = a
            p[2] = b
            ic = intcode.computer(p)
            ic.run()
            p = ic.get_program()
            answer = p[0]
Beispiel #15
0
def compute(program, inp):
    cpu = intcode.computer(program)
    next(cpu)
    return cpu.send(inp)
Beispiel #16
0
def find_intersections(p):
    rows = "".join([chr(o) for o in intcode.computer(p)]).split("\n")
    w, h = len(rows[0]), len(rows) - 2
    return sum([x*y for x in range(1, w-1) for y in range(1, h-1)
        if rows[y][x] == rows[y][x-1] == rows[y][x+1] ==
           rows[y-1][x] == rows[y+1][x] == '#'])
Beispiel #17
0
        # find all directions that contain space
        for d in [1, 3, 2, 4]:
            if grid[get_position(p, d)] == 2:
                new_pos = get_position(p, d)
                grid[new_pos] = 5
                stack.append(new_pos)

    if stack:
        loops += 1
        find_time_to_flood_chamber(stack)

    return


if __name__ == '__main__':
    ic = intcode.computer(prog)

    find_oxygen_tank(ic)
    render_grid(None)

    ic = intcode.computer(prog)
    find_path_to_oxygen_tank(ic)
    print("Part 1:", steps_to_find, "steps to oxygen tank")

    # reset grid
    grid[start] = 2
    for p, v in grid.items():
        if v == 5:
            grid[p] = 2

    render_grid(None)
Beispiel #18
0
def play(p):
    cpu = intcode.computer(p)
    print(readline(cpu))
    while True:
        print(send(cpu, sys.stdin.readline()))