Example #1
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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
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()))