示例#1
0
    def run(self):
        print(self)
        if self.input is None:
            raise Exception("Set the input value")
        #  https://stackoverflow.com/questions/16571150/how-to-capture-stdout-output-from-a-python-function-call
        output = []
        with Capturing(output) as output:
            run(self.int_code, Input([self.phase_setting, self.input]))

        self.set_output(int(output[0]))

        print(f"\tOutputting: {self.output}")
示例#2
0
def main() -> None:
    with open(INPUT, "r") as fin:
        program = [int(x) for x in fin.readline().strip().split(",")]

    max_output = None
    for order in itertools.permutations(range(5)):
        signal = 0
        for phase_setting in order:
            result = run(program[:], [phase_setting, signal])
            if result is None:
                raise Exception("No return value")
            signal = result
        max_output = signal if max_output is None else max(signal, max_output)
    print(max_output)

    max_output = None
    for order in itertools.permutations(range(5, 10)):
        signal = 0
        current_amplifier = 0
        amplifiers = []
        for phase_setting in order:
            p = Program(program[:], [phase_setting])
            amplifiers.append(p)

        while True:
            amplifiers[current_amplifier].send_input(signal)
            new_signal = amplifiers[current_amplifier].next_output_or_end()
            if new_signal is None:
                break
            signal = new_signal
            current_amplifier = (current_amplifier + 1) % len(amplifiers)
        max_output = signal if max_output is None else max(signal, max_output)
    print(max_output)
示例#3
0
def main() -> None:
    with open(INPUT, "r") as fin:
        program = [int(x) for x in fin.readline().split(",")]
        print(run(program[:], [1]))
        print(run(program[:], [2]))
示例#4
0
def run_program_with_args(program: List[int], noun: int, verb: int) -> int:
    program[1] = noun
    program[2] = verb
    program_copy = program[:]
    run(program_copy)
    return program_copy[0]