Exemplo n.º 1
0
def part2(file):
    tapes = [Tape.from_file(file, input_values=[i]) for i in range(50)]
    nat = None
    seen = set()
    did = True
    while True:
        for i, tape in enumerate(tapes):
            result_iter = tape.run()
            dest = next(result_iter, -1)
            if dest == -1:
                continue
            did = True
            x = next(result_iter)
            y = next(result_iter)
            if dest == 255:
                nat = (x, y)
                continue
            tapes[dest].input_extend([x, y])
        if not did:
            _, y = nat
            if y in seen:
                return y
            seen.add(y)
            tapes[0].input_extend(nat)
        did = False
Exemplo n.º 2
0
def part1(file):
    program = ("NOT C J\n" "AND D J\n" "NOT A T\n" "OR T J\n" "WALK\n")
    # not C and D OR not A
    tape = Tape.from_file(file)
    tape.input_extend(program)
    # print(''.join(map(chr, tape.run())))
    return list(tape.run())[-1]
Exemplo n.º 3
0
def part1(file):
    tape = Tape.from_file(file)
    go = tape.run()
    coll = []
    for x in go:
        x = chr(x)
        coll.append(x)
        if ''.join(coll[-8:]) == "Command?":
            print(''.join(coll))
            result = input("> ") + "\n"
            tape.input_extend(result)
    print(''.join(coll))
Exemplo n.º 4
0
def part2(memory):
    dirs = {1: (0, -1), 2: (0, 1), 3: (-1, 0), 4: (1, 0)}
    # 0: The repair droid hit a wall. Its position has not changed.
    # 1: The repair droid has moved one step in the requested direction.
    # 2: The repair droid has moved one step in the requested direction; its new position is the location of the oxygen system.

    # north (1), south (2), west (3), and east (4).

    # Accept a movement command via an input instruction.
    # Send the movement command to the repair droid.
    # Wait for the repair droid to finish the movement operation.
    # Report on the status of the repair droid via an output instruction.

    walls = collections.defaultdict(int)
    walls[0, 0] = OPEN

    tape = Tape(memory)
    x, y = 0, 0
    outputs = tape.run()
    while True:
        direction = next_unknown(x, y, walls)
        # print(direction)
        if not direction:
            # pprint.pprint(walls)
            air = next(k for k, v in walls.items() if v == AIR)
            return steps_from(air, walls)
        tape.input_value = direction
        nx, ny = map(sum, zip(dirs[direction], (x, y)))
        # print("going to", nx, ny, direction)
        result = next(outputs)
        if result == 0:
            # print("bonk")
            # print(walls[nx, ny])
            walls[nx, ny] = WALL
        elif result == 1:
            walls[nx, ny] = OPEN
            x, y = nx, ny
        else:
            walls[nx, ny] = AIR
            x, y = nx, ny
Exemplo n.º 5
0
def part1(file):
    tapes = [Tape.from_file(file, input_values=[i]) for i in range(50)]
    while True:
        for i, tape in enumerate(tapes):
            result_iter = tape.run()
            dest = next(result_iter, -1)
            if dest == -1:
                continue
            x = next(result_iter)
            y = next(result_iter)
            if dest == 255:
                return y
            tapes[dest].input_extend([x, y])
Exemplo n.º 6
0
def part2(file):
    program = ("NOT C J\n"
               "AND D J\n"
               "AND H J\n"
               "NOT B T\n"
               "AND D T\n"
               "OR T J\n"
               "NOT A T\n"
               "OR T J\n"
               "RUN\n")

    # ((not C and D) AND H) OR (not B and D) OR not A
    tape = Tape.from_file(file)
    tape.input_extend(program)
    # print(''.join(map(chr, tape.run())))
    return list(tape.run())[-1]
Exemplo n.º 7
0
def part2(file):
    board = get_board(file)

    tape = Tape.from_file(file)
    tape._memory[0] = 2

    path = fetch_path(board.split("\n"))
    # print(get_compressed(path))

    # substring = chunk_to_substrings(path)
    # print(substring)

    tape.input_extend("A,B,A,B,C,C,B,A,B,C\n"
                      "L,12,L,6,L,8,R,6\n"
                      "L,8,L,8,R,4,R,6,R,6\n"
                      "L,12,R,6,L,8\n"
                      "n\n")
    *result, score = tape.run()
    # Program output:
    # print("".join(map(chr, result)))
    return score
Exemplo n.º 8
0
def get_num(file, x, y):
    tape = Tape.from_file(file, input_values=[x, y])
    [result] = tape.run()
    return result
Exemplo n.º 9
0
def part1(file):
    tape = Tape.from_file(file)
    game = game_generator(tape)

    rooms = {}
    x, y = 0, 0

    opposite = {
        "west": "east",
        "south": "north",
        "north": "south",
        "east": "west",
    }
    without = None
    room = next(game)
    while True:
        name = room.strip().partition("\n")[0]
        if name.startswith("==") and name not in rooms:
            rooms[name] = ({
                d
                for d in ("north", "south", "east", "west") if d in room
            }, without)
            rooms[name][0].discard(without)

        for item in get_safe_items(room):
            game.send(f"take {item}")

        dirs, back = rooms[name]
        if dirs:
            direction = dirs.pop()
            room = game.send(direction)
            without = opposite[direction]
        else:
            if back is None:
                break
            room = game.send(back)

    game.send("north")
    game.send("north")
    game.send("west")
    game.send("north")
    game.send("west")

    all_items = {
        "mutex",
        "polygon",
        "jam",
        "prime number",
        "hologram",
        "semiconductor",
        "monolith",
        "weather machine",
    }
    for remove, add in powerset_delta(all_items):
        if add:
            game.send(f"take {add}")
        else:
            game.send(f"drop {remove}")
        result = game.send("north")
        if "Alert!" not in result:
            return result
Exemplo n.º 10
0
def main(memory):
    robot = Tape(memory)
    explore_area(robot)
Exemplo n.º 11
0
def part1(file):
    tape = Tape.from_file(file)
    go = tape.run()
    rooms = {}
    x, y = 0, 0

    bad = [
        "photons",
        "giant electromagnet",
        "infinite loop",
        "escape pod",
        "molten lava",
    ]
    opposite = {
        "west": "east",
        "south": "north",
        "north": "south",
        "east": "west",
    }
    without = None
    while True:
        room = get_to_command(go)
        name = room.strip().partition("\n")[0]
        if name.startswith("==") and name not in rooms:
            rooms[name] = ({
                d
                for d in ("north", "south", "east", "west") if d in room
            }, without)
            rooms[name][0].discard(without)
        debug(room)
        item_text = room.split("Items here:")
        items = []
        if len(item_text) > 1:
            items = item_text[1].split("\n")

        for i in items:
            if i.startswith("-"):
                i = i.lstrip("- ")
                if i in bad:
                    continue
                cmd = "take " + i + "\n"
                debug(cmd)
                tape.input_extend(cmd)
                get_to_command(go)
        else:
            dirs, back = rooms[name]
            if dirs:
                direction = dirs.pop()
                debug(direction)
                tape.input_extend(f"{direction}\n")
                without = opposite[direction]
            else:
                if back is None:
                    break
                tape.input_extend(f"{back}\n")

    tape.input_extend("north\nnorth\nwest\nnorth\nwest\n")
    for _ in range(5):
        debug(get_to_command(go))

    all_items = [
        "mutex",
        "polygon",
        "jam",
        "prime number",
        "hologram",
        "semiconductor",
        "monolith",
        "weather machine",
    ]
    for mset in powerset(all_items):
        count = len(all_items) + len(mset) + 1
        tape.input_extend(''.join(f"drop {i}\n" for i in all_items))
        tape.input_extend(''.join(f"take {i}\n" for i in mset))
        tape.input_extend("north\n")

        for _ in range(count):
            room = get_to_command(go)
            debug(room)
Exemplo n.º 12
0
def get_board(file):
    tape = Tape.from_file(file)
    return ''.join(map(chr, tape.run())).strip()