def test_opcode_4_provides_output(): output = Mock() computer = OpcodeComputer([4, 0, 99], output=output) computer.run() output.assert_called_with(4)
def test_opcode_3_makes_read(): input_src = iter([55]) computer = OpcodeComputer([3, 0, 99], input_src=input_src) computer.run() assert computer.memory == [55, 0, 99]
def test_equals_example(input, expected): computer = OpcodeComputer([3, 3, 1108, -1, 8, 3, 4, 3, 99]) computer.input_src = iter([input]) computer.output = Mock() computer.run() computer.output.assert_called_with(expected)
def test_lt_example(input, expected): computer = OpcodeComputer( [3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9]) computer.input_src = iter([input]) computer.output = Mock() computer.run() computer.output.assert_called_with(expected)
def part_a(): """Part A""" oc = OpcodeComputer(lines) # set value at *Position* to `value` with mode being 1 because argument 1 is an address that needs to be # written to in immediate mode oc.set_value(1, 12, 1) oc.set_value(2, 2, 1) oc.run() return oc.get_memory()[0]
def test_large_example(input, expected): computer = OpcodeComputer([ 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 ]) computer.input_src = iter([input]) computer.output = Mock() computer.run() computer.output.assert_called_with(expected)
def part_b(): """Part B""" target = 19690720 oc = OpcodeComputer(lines) for noun in range(0, 100): for verb in range(0, 100): # set value at *Position* to `value` with mode being 1 because argument 1 is an address that needs to be # written to in immediate mode oc.set_value(1, noun, 1) oc.set_value(2, verb, 1) oc.run() # if target reached if target == oc.get_memory()[0]: # return noun and verb (NNVV) return 100 * noun + verb # reset memory for another run oc.reset()
def test_immediate_mode_homepage_example(): computer = OpcodeComputer([1002, 4, 3, 4, 33]) computer.run() assert computer.memory == [1002, 4, 3, 4, 99]
def test_adding_two_numbers_in_direct_mode(): computer = OpcodeComputer([1101, 2, 3, 0, 99]) computer.run() assert computer.memory == [5, 2, 3, 0, 99]
def test_stopping_immediately_changes_nothing(): computer = OpcodeComputer([99, 1, 2, 3]) computer.run() assert computer.memory == [99, 1, 2, 3]
def test_homepage_examples(input, expected): computer = OpcodeComputer(input) computer.run() assert computer.memory == expected
def test_opcode_two_then_halting_multiplies_numbers(): computer = OpcodeComputer([2, 5, 6, 7, 99, 4, 6, 0]) computer.run() assert computer.memory == [2, 5, 6, 7, 99, 4, 6, 24]
def test_opcode_one_then_halting_adds_numbers(): computer = OpcodeComputer([1, 5, 6, 7, 99, 4, 6, 0]) computer.run() assert computer.memory == [1, 5, 6, 7, 99, 4, 6, 10]
def test_simple_jump(): computer = OpcodeComputer([1106, 0, 6, 104, 1, 99, 104, 2, 99]) computer.output = Mock() computer.run() computer.output.assert_called_with(2)
def part_b(): """Part B""" oc = OpcodeComputer(lines, [5]) oc.run() return oc.get_outputs()
def part_a(): oc = OpcodeComputer(lines, [1]) oc.run() return oc.get_outputs()[-1:]
from opcode_computer import OpcodeComputer from helpers import get_data data = get_data(day=5) program_master = [int(entry) for entry in data.split(',')] computer = OpcodeComputer(list(program_master), input_src=iter([1]), output=print) computer.run() # Restart computer, use 5 as input computer = OpcodeComputer(list(program_master), input_src=iter([5]), output=print) computer.run()
def test_opcode_execute_second_after_first(): computer = OpcodeComputer([1, 0, 0, 0, 2, 0, 0, 0, 99]) computer.run() assert computer.memory == [4, 0, 0, 0, 2, 0, 0, 0, 99]