Example #1
0
def solve_part_2(puzzle_input):
    #
    instructions = [
        "NOT A T",
        "OR T J",
        "NOT B T",
        "OR T J",
        "NOT C T",
        "OR T J",
        "AND D J",
        "NOT E T",
        "NOT T T",
        "OR H T",
        "AND T J",
    ]

    runtime = IntCodeRuntime()
    runtime.set_program(puzzle_input)
    runtime.run()
    print(runtime.get_ascii_output())
    runtime.input_ascii("\n".join(instructions))
    runtime.input_ascii("\nRUN\n")
    runtime.run()

    output = runtime.get_output()
    for num in output:
        try:
            print(chr(num), end="")
        except:
            return num
    return ""
Example #2
0
def solve_part_2(puzzle_input):
    amplifier_configurations = permutations(range(5, 10))

    highest_signal = 0
    best_configuration = None
    for configuration in amplifier_configurations:
        #print(configuration)
        signal = 0

        amp_runtimes = []
        for amp_i in range(5):
            amp_config = configuration[amp_i]
            program = puzzle_input[:]
            runtime = IntCodeRuntime()
            runtime.set_program(program)
            runtime.input_number(amp_config)
            amp_runtimes.append(runtime)

        while all(r.get_status() != IntCodeStatus.HALTED
                  for r in amp_runtimes):
            for amp_i, runtime in enumerate(amp_runtimes):
                if runtime.get_status() == IntCodeStatus.HALTED:
                    break

                runtime.input_number(signal)
                runtime.run()
                signal = runtime.get_output()[0]

        if signal > highest_signal:
            highest_signal = signal
            best_configuration = configuration

    return highest_signal
Example #3
0
def solve_part_1(puzzle_input):
    nics: List[IntCodeRuntime] = []
    for i in range(50):
        runtime = IntCodeRuntime()
        runtime.set_program(puzzle_input[:])
        runtime.run()
        runtime.input_number(i)
        runtime.run()
        nics.append(runtime)

    final_output = None
    while final_output is None:
        for nic in nics:
            if len(nic._input) == 0:
                nic.input_number(-1)
            nic.run()
            output = nic.get_output()
            for p in range(0, len(output), 3):
                address, x, y = output[p:p + 3]

                if address == 255:
                    final_output = y
                elif address < 50:
                    nics[address].input_number(x)
                    nics[address].input_number(y)
                else:
                    raise Exception(f"Invalid address {address}")

    return final_output
Example #4
0
def solve_part_2(puzzle_input):
    # Not hard to do the paths by hand
    PART_2_PATH = "R,12,L,8,L,4,L,4,L,8,R,6,L,6,R,12,L,8,L,4,L,4,L,8,R,6,L,6,L,8,L,4,R,12,L,6,L,4,R,12,L,8,L,4,L,4,L,8,L,4,R,12,L,6,L,4,R,12,L,8,L,4,L,4,L,8,L,4,R,12,L,6,L,4,L,8,R,6,L,6"
    sub_paths = {
        "A": "R,12,L,8,L,4,L,4",
        "B": "L,8,L,4,R,12,L,6,L,4",
        "C": "L,8,R,6,L,6"
    }

    main_path = "A,C,A,C,B,A,B,A,B,C"

    logic_input = [main_path, sub_paths["A"], sub_paths["B"], sub_paths["C"], "n"]

    program = puzzle_input[:]
    program[0] = 2
    runtime = IntCodeRuntime()
    runtime.set_program(program)

    for line in logic_input:
        for char in line:
            runtime.input_number(ord(char))
        runtime.input_number(ord("\n"))

    runtime.run()

    return runtime.get_output()[-1]
Example #5
0
def solve_part_1(puzzle_input):
    runtime = IntCodeRuntime()
    runtime.set_program(puzzle_input[:])
    runtime.run()

    output = runtime.get_output()
    ascii_output = "".join(chr(code) for code in output)
    print(ascii_output)

    scaffold_map = []
    current_line = []
    for char in ascii_output:
        if char == "\n":
            scaffold_map.append(current_line)
            current_line = []
        else:
            current_line.append(char)

    alignment_sum = 0
    for j in range(1,len(scaffold_map)-2):
        for i in range(1, len(scaffold_map[j])-1):
            if scaffold_map[j][i] == "#" and \
               scaffold_map[j][i+1] == "#" and scaffold_map[j+1][i] == "#" and \
               scaffold_map[j][i-1] == "#" and scaffold_map[j-1][i] == "#":
            
                alignment_sum += j * i
    
    return alignment_sum
Example #6
0
def solve_part_1(puzzle_input, system_id):
    runtime = IntCodeRuntime()
    runtime.set_program(puzzle_input)
    runtime.input_number(system_id)
    runtime.run()
    for line in runtime.get_output():
        print(line)
Example #7
0
def solve_part_1(puzzle_input):
    runtime = IntCodeRuntime()
    runtime.set_program(puzzle_input[:])
    runtime.run()

    droid = DroidController(runtime)
    distance = droid.explore(0)
    return distance, runtime
Example #8
0
def get_item_inventory(runtime: IntCodeRuntime):
    runtime.input_ascii("inv\n")
    runtime.run()
    output = runtime.get_ascii_output()
    print(output, end="")

    items = []
    for line in output.split("\n"):
        if line.startswith("- "):
            items.append(line.split(" ", 1)[1])
    return items
Example #9
0
def solve_part_1(puzzle_input):
    runtime = IntCodeRuntime()
    runtime.set_program(puzzle_input[:])
    runtime.run()

    output = runtime.get_output()
    tile_ids = output[2::3]
    count_blocks = 0
    for tile_id in tile_ids:
        if tile_id == 2:
            count_blocks += 1

    return count_blocks
Example #10
0
def solve_part_2(puzzle_input):
    nics: List[IntCodeRuntime] = []
    for i in range(50):
        runtime = IntCodeRuntime()
        runtime.set_program(puzzle_input[:])
        runtime.run()
        runtime.input_number(i)
        runtime.run()
        nics.append(runtime)

    nat_x = None
    nat_y = None
    last_restart_y = None
    final_output = None
    while final_output is None:
        idle = True

        for nic in nics:
            if len(nic._input) == 0:
                nic.input_number(-1)
            else:
                idle = False
            nic.run()
            output = nic.get_output()
            for p in range(0, len(output), 3):
                idle = False
                address, x, y = output[p:p + 3]

                if address == 255:
                    nat_x = x
                    nat_y = y
                elif address < 50:
                    nics[address].input_number(x)
                    nics[address].input_number(y)
                else:
                    raise Exception(f"Invalid address {address}")

        if idle is True:
            if nat_y == last_restart_y:
                final_output = nat_y
            nics[0].input_number(nat_x)
            nics[0].input_number(nat_y)
            last_restart_y = nat_y

    return final_output
Example #11
0
def solve_part_2(puzzle_input):
    puzzle_input[0] = 2
    runtime = IntCodeRuntime()
    runtime.set_program(puzzle_input[:])
    screen = [[None for _ in range(45)] for __ in range(24)]
    segment_display = 0

    while runtime.get_status() != IntCodeStatus.HALTED:
        runtime.run()
        output = runtime.get_output()
        segment_display = refresh_screen(screen, segment_display, output)
        #joystick_input = input("Joystick Input: ")
        sleep(1 / 60)
        joystick_input = get_joystick_input(screen)
        joystick_signal = joystick_signal_map[joystick_input]
        runtime.input_number(joystick_signal)

    return "Game Over"
Example #12
0
def solve_part_1(puzzle_input, macros):
    runtime = IntCodeRuntime()
    runtime.set_program(puzzle_input[:])
    recording = None
    while runtime.get_status() != IntCodeStatus.HALTED:
        runtime.run()
        print(runtime.get_ascii_output(), end="")

        command = input()
        if recording is not None:
            macros[recording].append(command)

        if command.startswith("record"):
            name = command.split()[-1]
            macros[name] = []
            recording = name
            print("")
            print(f"Recording macro `{name}`")
            print("")
            print("Command?")

        elif command.startswith("macro"):
            name = command.split()[-1]
            run_macro(runtime, macros[name])

        elif command.startswith("stop record"):
            print("")
            print(f"Stopping record of macro `{recording}`")
            print("")
            del macros[recording][-1]
            print("Macro saved with:")
            for item in macros[recording]:
                print(f"- {item}")
            recording = None
            print("")
            print("Command?")

        elif command.startswith("hack sensor"):
            hack_sensor(runtime)

        else:
            runtime.input_ascii(command + "\n")

    return ""
Example #13
0
def solve(puzzle_input, start_square=0):
    painted = set()
    grid = defaultdict(int)

    runtime = IntCodeRuntime()
    runtime.set_program(puzzle_input[:])

    x, y = (0, 0)
    grid[(x, y)] = start_square
    dx, dy = NORTH
    while runtime.get_status() != IntCodeStatus.HALTED:
        runtime.input_number(grid[(x, y)])
        runtime.run()
        output = runtime.get_output()
        assert len(output) == 2
        grid[(x, y)] = output[0]
        painted.add((x, y))
        dx, dy = get_new_direction((dx, dy), output[1])
        x, y = (x + dx, y + dy)

    return len(painted), grid
Example #14
0
def hack_sensor(runtime: IntCodeRuntime):
    print("")
    print("Hacking sensor..")
    print("Checking inventory..")
    items = get_item_inventory(runtime)
    print(f"Found {len(items)} items.")

    combinations = pow(2, len(items))
    print(f"Checking against {combinations} possible combinations.")

    sucessful_combinations = []
    for i in range(combinations):
        for item in get_item_inventory(runtime):
            runtime.input_ascii(f"drop {item}\n")
            runtime.run()
            print(runtime.get_ascii_output(), end="")

        selectors = (1 if (1 << shift) & i else 0
                     for shift in range(len(items)))
        selections = list(compress(items, selectors))

        for item in selections:
            runtime.input_ascii(f"take {item}\n")
            runtime.run()
            print(runtime.get_ascii_output(), end="")

        runtime.input_ascii(f"south\n")
        runtime.run()
        output = runtime.get_ascii_output()
        print(output, end="")

        made_it = True
        for line in output.split("\n"):
            if "Alert!" in line:
                made_it = False

        if made_it:
            sucessful_combinations.append(selections)
            runtime.input_ascii(f"north\n")
            runtime.run()
            print(runtime.get_ascii_output())

    for combination in sucessful_combinations:
        print("Successful combination: ", list(combination))

    print("")
    print("Command?")
Example #15
0
def solve_part_1(puzzle_input):
    amplifier_configurations = permutations(range(5))

    highest_signal = 0
    best_configuration = None
    for configuration in amplifier_configurations:
        #print(configuration)
        signal = 0
        for amp_i in range(5):
            amp_config = configuration[amp_i]
            program = puzzle_input[:]

            runtime = IntCodeRuntime()
            runtime.set_program(program)
            runtime.input_number(amp_config)
            runtime.input_number(signal)
            runtime.run()
            signal = runtime.get_output()[0]

        if signal > highest_signal:
            highest_signal = signal
            best_configuration = configuration

    return highest_signal
Example #16
0
def run_macro(runtime: IntCodeRuntime, macro: List[str]):
    for command in macro:
        runtime.input_ascii(command + "\n")
        runtime.run()
        print(runtime.get_ascii_output(), end="")
Example #17
0
def solve_part_1(puzzle_input):
    runtime = IntCodeRuntime()
    runtime.set_program(puzzle_input[:])
    runtime.input_number(1)
    runtime.run()
    return "\n" + "\n".join(map(str, runtime.get_output()))