Esempio n. 1
0
def test_day_05_example_08():
    original_memory = [3, 21, 1008, 21, 8, 20, 1005, 20, 22, 107, 8, 21, 20, 1006, 20, 31, 1106, 0, 36, 98, 0, 0, 1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999, 1105, 1, 46, 1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99]  # noqa: E501
    input_buffer_1 = queue.Queue()
    input_buffer_1.put(7)
    output_buffer_1 = queue.Queue()
    ss_1 = Intcode(input_device=input_buffer_1, output_device=output_buffer_1)
    memory = list(original_memory)
    ss_1.load_program(memory)
    ss_1.run_program()
    assert output_buffer_1.get() == 999
    input_buffer_2 = queue.Queue()
    input_buffer_2.put(8)
    output_buffer_2 = queue.Queue()
    ss_2 = Intcode(input_device=input_buffer_2, output_device=output_buffer_2)
    memory = list(original_memory)
    ss_2.load_program(memory)
    ss_2.run_program()
    assert output_buffer_2.get() == 1000
    input_buffer_3 = queue.Queue()
    input_buffer_3.put(9)
    output_buffer_3 = queue.Queue()
    ss_3 = Intcode(input_device=input_buffer_3, output_device=output_buffer_3)
    memory = list(original_memory)
    ss_3.load_program(memory)
    ss_3.run_program()
    assert output_buffer_3.get() == 1001
Esempio n. 2
0
def test_jump_if_false_position():
    program = [6, 3, 2, 0]
    ss = Intcode()
    ss.load_program(program)
    will_jump = JumpIfFalse(ss)
    will_jump.act()
    assert ss.instr_ptr == 2
    program = [6, 3, 2, 1]
    ss = Intcode()
    ss.load_program(program)
    wont_jump = JumpIfFalse(ss)
    wont_jump.act()
    assert ss.instr_ptr == 3
Esempio n. 3
0
def test_jump_if_false_immediate():
    program = [1106, 0, 2]
    ss = Intcode()
    ss.load_program(program)
    will_jump = JumpIfFalse(ss)
    will_jump.act()
    assert ss.instr_ptr == 2
    program = [1106, 1, 2]
    ss = Intcode()
    ss.load_program(program)
    wont_jump = JumpIfFalse(ss)
    wont_jump.act()
    assert ss.instr_ptr == 3
Esempio n. 4
0
def test_jump_if_true_immediate():
    program = [1105, 1, 2]
    ss = Intcode()
    ss.load_program(program)
    will_true = JumpIfTrue(ss)
    will_true.act()
    assert ss.instr_ptr == 2
    program = [1105, 0, 2]
    ss = Intcode()
    ss.load_program(program)
    wont_jump = JumpIfTrue(ss)
    wont_jump.act()
    assert ss.instr_ptr == 3
Esempio n. 5
0
def test_jump_if_true_position():
    program = [5, 3, 2, 1]
    ss = Intcode()
    ss.load_program(program)
    jump_true = JumpIfTrue(ss)
    jump_true.act()
    assert ss.instr_ptr == 2
    program = [5, 3, 2, 0]
    ss = Intcode()
    ss.load_program(program)
    jump_false = JumpIfTrue(ss)
    jump_false.act()
    assert ss.instr_ptr == 3
Esempio n. 6
0
def test_less_than_relative_mode():
    program = [22207, 0, 1, 2, 1, 2, -1]
    ss = Intcode()
    ss.load_program(program)
    ss.relative_base = 4
    lt_true = LessThan(ss)
    lt_true.act()
    assert ss.read_memory(6) == 1
    program = [22207, 0, 1, 2, 3, 2, -1]
    ss = Intcode()
    ss.load_program(program)
    ss.relative_base = 4
    lt_false = LessThan(ss)
    lt_false.act()
    assert ss.read_memory(6) == 0
Esempio n. 7
0
def test_equal_immediate():
    program = [1108, 0, 2, 4, 0]
    ss = Intcode()
    ss.load_program(program)
    less_than_true = Equals(ss)
    less_than_true.act()
    assert ss.instr_ptr == 4
    assert ss.read_memory(4) == 0
    program = [1108, 2, 2, 4, 0]
    ss = Intcode()
    ss.load_program(program)
    less_than_false = Equals(ss)
    less_than_false.act()
    assert ss.instr_ptr == 4
    assert ss.read_memory(4) == 1
Esempio n. 8
0
def test_less_than_immediate():
    program = [1107, 0, 2, 4, 0]
    ss = Intcode()
    ss.load_program(program)
    less_than_true = LessThan(ss)
    less_than_true.act()
    assert ss.instr_ptr == 4
    assert ss.read_memory(4) == 1
    program = [1107, 4, 2, 4, 0]
    ss = Intcode()
    ss.load_program(program)
    less_than_false = LessThan(ss)
    less_than_false.act()
    assert ss.instr_ptr == 4
    assert ss.read_memory(4) == 0
Esempio n. 9
0
def test_equal_position():
    program = [8, 0, 2, 4, 0]
    ss = Intcode()
    ss.load_program(program)
    less_than_true = Equals(ss)
    less_than_true.act()
    assert ss.instr_ptr == 4
    assert ss.read_memory(4) == 0
    program = [8, 4, 5, 6, 0, 0, 0]
    ss = Intcode()
    ss.load_program(program)
    less_than_false = Equals(ss)
    less_than_false.act()
    assert ss.instr_ptr == 4
    assert ss.read_memory(6) == 1
Esempio n. 10
0
def test_equals_relative_mode():
    program = [22208, 0, 1, 2, 1, 1, -1]
    ss = Intcode()
    ss.load_program(program)
    ss.relative_base = 4
    eq_true = Equals(ss)
    eq_true.act()
    assert ss.read_memory(6) == 1
    program = [22208, 0, 1, 2, 3, 2, -1]
    ss = Intcode()
    ss.load_program(program)
    ss.relative_base = 4
    eq_false = Equals(ss)
    eq_false.act()
    assert ss.read_memory(6) == 0
Esempio n. 11
0
def test_stop():
    program = [99]
    ss = Intcode()
    ss.load_program(program)
    stop = Stop(ss)
    stop.act()
    assert ss.instr_ptr == -1
Esempio n. 12
0
def run1202(noun: int, verb: int):
    num = task_input.copy()
    num[1] = noun
    num[2] = verb
    task1 = Intcode(num)
    task1()
    return num[0]
Esempio n. 13
0
def test_day_09_example_03():
    program = [104, 1125899906842624, 99]
    output_buffer = queue.Queue()
    ss = Intcode(output_device=output_buffer)
    ss.load_program(program)
    ss.run_program()
    assert output_buffer.get() == program[1]
Esempio n. 14
0
def try_direction(my_program, path_to_try):
    this_result_buffer = LifoQueue()
    this_runner = Intcode(input_device=path_to_try,
                          output_device=this_result_buffer)
    this_runner.load_program(my_program)
    this_runner.run_program()
    return this_result_buffer.get()
Esempio n. 15
0
def test_day_09_example_02():
    program = [1102, 34915192, 34915192, 7, 4, 7, 99, 0]
    output_buffer = queue.Queue()
    ss = Intcode(output_device=output_buffer)
    ss.load_program(program)
    ss.run_program()
    assert len(str(output_buffer.get())) == 16
Esempio n. 16
0
def test_multiplication_immediate_mode():
    program = [1102, 2, 2, 4, 0]
    ss = Intcode()
    ss.load_program(program)
    mult_immediate = Multiplication(ss)
    mult_immediate.act()
    assert ss.instr_ptr == 4
    assert ss.read_memory(4) == 4
Esempio n. 17
0
def test_multiplication_position_mode():
    program = [2, 0, 2, 4, 0]
    ss = Intcode()
    ss.load_program(program)
    mult_position = Multiplication(ss)
    mult_position.act()
    assert ss.instr_ptr == 4
    assert ss.read_memory(4) == 4
Esempio n. 18
0
def test_day_09_example_01():
    program = [109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99]
    output_buffer = queue.Queue()
    ss = Intcode(output_device=output_buffer)
    ss.load_program(list(program))
    ss.run_program()
    for i in range(len(program)):
        assert output_buffer.get() == program[i]
Esempio n. 19
0
def test_less_than_cannot_have_destination_parameter_in_IMMEDIATE_mode():
    ss = Intcode()
    ss.load_program([11107, 1, 2, 4, 0])
    try:
        LessThan(ss)
        assert False
    except ValueError:
        assert True
Esempio n. 20
0
def test_addition_relative_mode():
    program = [22201, 0, 1, 2, 1, 2, 0]
    ss = Intcode()
    ss.load_program(program)
    ss.relative_base = 4
    adder = Addition(ss)
    adder.act()
    assert ss.read_memory(6) == 3
Esempio n. 21
0
def test_day_04_part_2():
    memory = Intcode.read_program('../02/input.txt')
    memory[1] = 67
    memory[2] = 18
    ss = Intcode()
    ss.load_program(memory)
    ss.run_program()
    assert ss.program[0] == 19690720
Esempio n. 22
0
def test_day_04_corrected_input():
    memory = Intcode.read_program('../02/input.txt')
    memory[1] = 12
    memory[2] = 2
    ss = Intcode()
    ss.load_program(memory)
    ss.run_program()
    assert ss.program[0] == 3850704
Esempio n. 23
0
def test_relative_base_relative():
    program = [209, 0, -69]
    ss = Intcode()
    ss.relative_base = 70
    ss.load_program(program)
    relative_base = RelativeBase(ss)
    relative_base.act()
    assert ss.relative_base == 70
Esempio n. 24
0
def test_addition_immediate_mode():
    program = [1101, 1, 2, 4, 0]
    ss = Intcode()
    ss.load_program(program)
    adder_immediate = Addition(ss)
    adder_immediate.act()
    assert ss.instr_ptr == 4
    assert ss.read_memory(4) == 3
Esempio n. 25
0
def test_relative_base_immediate():
    program = [109, 19]
    ss = Intcode()
    ss.relative_base = 5
    ss.load_program(program)
    relative_base = RelativeBase(ss)
    relative_base.act()
    assert ss.relative_base == 24
Esempio n. 26
0
def test_multiplication_cannot_have_destination_parameter_in_IMMEDIATE_mode():
    ss = Intcode()
    ss.load_program([11102, 1, 2, 4, 0])
    try:
        Multiplication(ss)
        assert False
    except ValueError:
        assert True
Esempio n. 27
0
def test_equals_cannot_have_destination_parameter_in_IMMEDIATE_mode():
    ss = Intcode()
    ss.load_program([11108, 1, 2, 4, 0])
    try:
        Equals(ss)
        assert False
    except ValueError:
        assert True
Esempio n. 28
0
def test_addition_position_mode():
    program = [1, 0, 1, 4, 0]
    ss = Intcode()
    ss.load_program(program)
    adder = Addition(ss)
    adder.act()
    assert ss.instr_ptr == 4
    assert ss.read_memory(4) == 1
Esempio n. 29
0
def test_multiplication_relative_mode():
    program = [22202, 0, 1, 2, 1, 2, 0]
    ss = Intcode()
    ss.load_program(program)
    ss.relative_base = 4
    multiplier = Multiplication(ss)
    multiplier.act()
    assert ss.read_memory(6) == 2
Esempio n. 30
0
def test_relative_base_position():
    program = [9, 2, -7]
    ss = Intcode()
    ss.relative_base = 10
    ss.load_program(program)
    relative_base = RelativeBase(ss)
    relative_base.act()
    assert ss.relative_base == 3