Пример #1
0
def part1(filename):
    source = intcode.load_from_file(filename)
    i, o = [], []
    # AC Unit input value
    i.append(1)
    modified = intcode.run_intcode(source, i, o)
    return modified[0], i, o
Пример #2
0
def run_amplifier_loop(program: Program, phases: List) -> int:

    current_programs = [program[:] for _ in range(len(phases))]
    current_cursors = [0 for _ in range(len(phases))]

    current_input = 0
    current_non_none_input = None
    n_halted = 0
    i = 0

    while n_halted < len(phases):
        amp_program = current_programs[i % len(phases)]
        amp_cursor = current_cursors[i % len(phases)]
        if i < len(phases):
            inputs = [phases[i % len(phases)], current_input]
        else:
            inputs = [current_input]
        output, cursor = run_intcode(amp_program,
                                     cursor=amp_cursor,
                                     inputs=inputs)
        if output is None:
            n_halted += 1
        else:
            current_non_none_input = output
        current_input = output

        current_cursors[i % len(phases)] = cursor
        i += 1

    return current_non_none_input
Пример #3
0
def part2(filename):
    source = intcode.load_from_file(filename)
    i, o = [], []
    # Thermal Radiator Controller
    i.append(5)
    modified = intcode.run_intcode(source, i, o)
    return modified[0], i, o
Пример #4
0
def main():
    with open(sys.argv[1], "r") as intcode_file:
        intcode = [int(code) for code in intcode_file.read().split(",")]

        droid = run_intcode(intcode)
        world_map = explore_world(droid)
        draw_image(parse_image(world_map))
        print("Minutes to fill:", get_longest_path(world_map) - 1)
Пример #5
0
def run_amplifier_sequence(program: Program, phases: List[int]) -> int:

    current_input = 0

    for i in range(len(phases)):
        output = run_intcode(program, inputs=[phases[i], current_input])[0]
        current_input = output

    return output
Пример #6
0
from intcode import run_intcode

intcode = sys.argv[1]
init_memmap = [int(i) for i in intcode.split(",")]

thrusters = []
for phase_sequence in permutations(range(5, 10)):

    signal = 0
    amplifiers = []

    # Instantiate all amplifiers according to phase sequence
    for phase in phase_sequence:
        inputs = [phase]
        amplifier = run_intcode(init_memmap, inputs)
        amplifiers.append((inputs, amplifier))

    # Inject feedback looped signal
    running = True
    while running:
        for (ampin, amplifier) in amplifiers:
            try:
                ampin.append(signal)
                signal = next(amplifier)
            except StopIteration:
                running = False
                continue

    thrusters.append((signal, phase_sequence))
Пример #7
0
#!/usr/bin/env python3

from itertools import permutations
import sys

from intcode import run_intcode

intcode = sys.argv[1]
init_memmap = [int(i) for i in intcode.split(",")]

thrusters = []
for phase_sequence in permutations(range(0,5)):

    signal = 0
    for phase in phase_sequence:
        signal = next(run_intcode(init_memmap, [phase, signal]))

    thrusters.append((signal, phase_sequence))

print(sorted(thrusters))
Пример #8
0
#!/usr/bin/env python3

from itertools import permutations
import sys
import code

from intcode import run_intcode

intcode = sys.argv[1]
init_memmap = [int(i) for i in intcode.split(",")]

computer = run_intcode(init_memmap, [2])

output = []
try:
    while True:
        output.append(next(computer))
except StopIteration as e:
    print(output)
    #code.interact(local=locals())