Example #1
0
def test_1():
    q1 = Queue()
    q1.put(1)
    q1.put(3)
    q1.put("hej")
    l = queue_to_list(q1)
    assert [1,3,"hej"]==l
Example #2
0
def calc_amp_with_feedback(amp_program: Program, phase_config: Sequence[int]):
    input_and_feedback_queue = Queue()
    input_and_feedback_queue.put(phase_config[0])
    input_and_feedback_queue.put(0)
    a_to_b_queue = Queue()
    a_to_b_queue.put(phase_config[1])
    b_to_c_queue = Queue()
    b_to_c_queue.put(phase_config[2])
    c_to_d_queue = Queue()
    c_to_d_queue.put(phase_config[3])
    d_to_e_queue = Queue()
    d_to_e_queue.put(phase_config[4])

    with concurrent.futures.ThreadPoolExecutor(max_workers=6) as executor:
        executor.submit(run_computer_on_queues, amp_program,
                        input_and_feedback_queue, a_to_b_queue)
        executor.submit(run_computer_on_queues, amp_program, a_to_b_queue,
                        b_to_c_queue)
        executor.submit(run_computer_on_queues, amp_program, b_to_c_queue,
                        c_to_d_queue)
        executor.submit(run_computer_on_queues, amp_program, c_to_d_queue,
                        d_to_e_queue)
        executor.submit(run_computer_on_queues, amp_program, d_to_e_queue,
                        input_and_feedback_queue)

    outs = queue_to_list(input_and_feedback_queue)

    return outs[-2]
Example #3
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
Example #4
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
Example #5
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
Example #6
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]
Example #7
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)
Example #8
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]
Example #9
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