Ejemplo n.º 1
0
def compute_computer_output(program: Program, noun: int, verb: int) -> int:
    initial_state = program.copy()
    initial_state[1] = noun
    initial_state[2] = verb
    cmp = Computer(initial_state, None, None)
    cmp.run_until_stop()
    return cmp._state[0]
Ejemplo n.º 2
0
def test_day9_test3():
    """output the large number in the middle"""
    prog = [104, 1125899906842624, 99]
    q2 = Queue()
    c = Computer(prog, None, q2)
    c.run_until_stop()
    output = queue_to_list(q2)[0]
    assert output== 1125899906842624
Ejemplo n.º 3
0
def test_day9_test1():
    """Can copy self?"""
    prog = [109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99]
    q2 = Queue()
    c = Computer(prog, None, q2)
    c.run_until_stop()
    outputs = [i for i in queue_to_list(q2) if not isinstance(i,ComputerSignals)]
    assert outputs== prog
Ejemplo n.º 4
0
def test_day9_test2():
    """Can output a 16-digit number?"""
    prog = [1102, 34915192, 34915192, 7, 4, 7, 99, 0]
    q2 = Queue()
    c = Computer(prog, None, q2)
    c.run_until_stop()
    output = queue_to_list(q2)[0]
    output_n_digit = len(str(output))
    assert output_n_digit== 16
Ejemplo n.º 5
0
def test_day5_test2():
    """TEST program runs okay"""
    p = thermal_environment_supervision_terminal()
    inval = 1
    q1 = Queue()
    q1.put(inval)
    q2 = Queue()
    c = Computer(p, q1, q2)
    c.run_until_stop()
    outputs = [i for i in queue_to_list(q2) if not isinstance(i,ComputerSignals)]
    diagnostic_codes = outputs[:-1]
    assert diagnostic_codes== [0, 0, 0, 0, 0, 0, 0, 0, 0]
Ejemplo n.º 6
0
def solve_part_one():
    p = get_program('../inputs/day13.txt')
    c = Computer(p, Queue(), Queue())
    c.run_until_stop()
    all_outputs = queue_to_list(c.output_queue)
    all_outputs = all_outputs[:
                              -1]  # skip the final "program terminated"-output

    screen = defaultdict(int)
    for x, y, tile_id in grouper(all_outputs, 3):

        assert tile_id in tile_ids, f"invalid code: {tile_id}"
        screen[(x, y)] = tile_id

    return sum(1 for tid in screen.values() if tid == BLOCK)
Ejemplo n.º 7
0
def calc_amp(amp_program: Program, phase_config: Iterable[int]):
    input_ = 0
    for phase in phase_config:
        inq = Queue()
        inq.put(phase)
        inq.put(input_)
        outq = Queue()
        amplifier = Computer(amp_program, input_queue=inq, output_queue=outq)
        amplifier.run_until_stop()
        amp_output = [
            i for i in queue_to_list(outq)
            if not isinstance(i, ComputerSignals)
        ]
        assert len(amp_output) == 1
        input_ = amp_output[-1]
    return amp_output[-1]
Ejemplo n.º 8
0
def run_computer_on_queues(program: Program, in_q: Queue, out_q: Queue):
    c = Computer(program, input_queue=in_q, output_queue=out_q)
    logging.debug("Starting execution of some program.")
    c.run_until_stop()
    logging.debug("The program has stopped.")
Ejemplo n.º 9
0
def test_day2_test5():
    instate = [1, 1, 1, 4, 99, 5, 6, 0, 99]
    outstate = [30, 1, 1, 4, 2, 5, 6, 0, 99]
    c = Computer(instate, None, None)
    c.run_until_stop()
    assert c.state== outstate
Ejemplo n.º 10
0
def test_day2_test4():
    instate = [2, 4, 4, 5, 99, 0]
    outstate = [2, 4, 4, 5, 99, 9801]
    c = Computer(instate, None, None)
    c.run_until_stop()
    assert c.state== outstate
Ejemplo n.º 11
0
def test_day2_test3():
    instate = [2, 3, 0, 3, 99]
    outstate = [2, 3, 0, 6, 99]
    c = Computer(instate, None, None)
    c.run_until_stop()
    assert c.state== outstate
Ejemplo n.º 12
0
from queue import Queue

from util import queue_to_list, thermal_environment_supervision_terminal, Computer

if __name__ == "__main__":
    p = thermal_environment_supervision_terminal()
    q1 = Queue()
    q2 = Queue()
    q1.put(1)
    c = Computer(p, q1, q2)
    c.run_until_stop()
    outputs = queue_to_list(q2)
    print(f"Ans A:{outputs[-2]}")  # 11049715

    q1 = Queue()
    q2 = Queue()
    q1.put(5)
    c = Computer(p, q1, q2)
    c.run_until_stop()
    outputs = queue_to_list(q2)
    print(f"Ans B:{outputs[-2]}")  # 2140710