def _read_recordings(): lines = _read_lines("recordings") while lines: before_str, params_str, after_str, *lines = lines before = _parse_numbers( before_str.replace("Before: [", "").replace("]", "").replace(",", "")) opcode, *params = _parse_numbers(params_str) after = _parse_numbers( after_str.replace("After: [", "").replace("]", "").replace(",", "")) yield Recording(before, after, opcode, OperationParams(*params))
def _parse_instruction(instruction_str: str) -> Instruction: name, val_a, val_b, val_c = instruction_str.split(" ") params = OperationParams(int(val_a), int(val_b), int(val_c)) return ALL[name], params
def test_gtrr_if_a_eq_b(self): gtrr(self.registers, OperationParams(1, 5, 3)) self.assertEqual(self.registers[3], 0)
def test_gtri_if_a_sm_b(self): gtri(self.registers, OperationParams(1, 4, 3)) self.assertEqual(self.registers[3], 1)
def test_gtir_if_a_sm_b(self): gtir(self.registers, OperationParams(4, 1, 3)) self.assertEqual(self.registers[3], 0)
def test_seti(self): seti(self.registers, OperationParams(42, -1, 1)) self.assertEqual(self.registers[1], 42)
def test_bori(self): bori(self.registers, OperationParams(3, 30, 1)) self.assertEqual(self.registers[1], 31) # 7|30 = 31
def test_eqri_if_equal(self): eqri(self.registers, OperationParams(1, 5, 3)) self.assertEqual(self.registers[3], 1)
def test_bani(self): bani(self.registers, OperationParams(3, 30, 1)) self.assertEqual(self.registers[1], 6) # 7&30 = 6
def test_banr(self): banr(self.registers, OperationParams(3, 4, 3)) self.assertEqual(self.registers[3], 6) # 7&30 = 6
def test_muli(self): muli(self.registers, OperationParams(1, 2, 3)) self.assertEqual(self.registers[3], 5 * 2)
def test_addi(self): addi(self.registers, OperationParams(1, 2, 3)) self.assertEqual(self.registers[3], 5 + 2)
def test_number_of_matching_operators(self): recording = Recording( before=(3, 2, 1, 1), after=(3, 2, 2, 1), opcode=-1, params=OperationParams(2, 1, 2) ) operations = matching_operations(recording) self.assertSetEqual(operations, set(["mulr", "addi", "seti"]))
def test_eqrr_if_different(self): eqrr(self.registers, OperationParams(1, 6, 3)) self.assertEqual(self.registers[3], 0)
def test_eqrr_if_equal(self): eqrr(self.registers, OperationParams(2, 6, 3)) self.assertEqual(self.registers[3], 1)
def _read_operations(mapping): for line in _read_lines("operations"): opcode, *params = _parse_numbers(line) yield (ALL[mapping[opcode]], OperationParams(*params))
def test_borr(self): borr(self.registers, OperationParams(3, 4, 3)) self.assertEqual(self.registers[3], 31) # 7|30 = 31
def test_eqir_if_equal(self): eqir(self.registers, OperationParams(5, 1, 3)) self.assertEqual(self.registers[3], 1)