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):
    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 #3
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 #4
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