def test_eql(self): instruction = decode("eql z w") self.assertEqual(instruction.type, InstructionType.EQUALS) self.assertEqual(instruction.source_register, "w") self.assertEqual(instruction.source_value, None) self.assertEqual(instruction.target_register, "z") instruction = decode("eql z 2") self.assertEqual(instruction.type, InstructionType.EQUALS) self.assertEqual(instruction.source_register, None) self.assertEqual(instruction.source_value, 2) self.assertEqual(instruction.target_register, "z")
def test_mod(self): instruction = decode("mod z w") self.assertEqual(instruction.type, InstructionType.MOD) self.assertEqual(instruction.source_register, "w") self.assertEqual(instruction.source_value, None) self.assertEqual(instruction.target_register, "z") instruction = decode("mod z 2") self.assertEqual(instruction.type, InstructionType.MOD) self.assertEqual(instruction.source_register, None) self.assertEqual(instruction.source_value, 2) self.assertEqual(instruction.target_register, "z")
def test_div(self): instruction = decode("div z w") self.assertEqual(instruction.type, InstructionType.DIV) self.assertEqual(instruction.source_register, "w") self.assertEqual(instruction.source_value, None) self.assertEqual(instruction.target_register, "z") self.assertEqual(instruction.source_value, None) instruction = decode("div z 2") self.assertEqual(instruction.type, InstructionType.DIV) self.assertEqual(instruction.source_register, None) self.assertEqual(instruction.source_value, 2) self.assertEqual(instruction.target_register, "z")
def test_input(self): instruction = decode("inp x") self.assertEqual(instruction.type, InstructionType.INPUT) self.assertEqual(instruction.source_register, None) self.assertEqual(instruction.source_value, None) self.assertEqual(instruction.target_register, "x") self.assertEqual(instruction.source_value, None)
def test_trivial_example(self): code = [decode(line) for line in ["inp x", "mul x -1"]] registers = track_dependencies(code) self.assertListEqual(registers.reg_w.inputs, []) self.assertListEqual(registers.reg_x.inputs, [0]) self.assertListEqual(registers.reg_y.inputs, []) self.assertListEqual(registers.reg_z.inputs, [])
def test_backtrack_two_below(self): code = [ decode(line) for line in [ "inp w", "inp w", "inp x", "inp x", "inp x", "eql w x", "eql x 2", "add z w", "add z x", "eql z 2", "eql z 0" ] ] max_n = reduce_bounds_from_below(code, [1, 1, 1, 1, 1, 1, 1]) self.assertListEqual(max_n, [1, 2, 1, 1, 2, 1, 1])
def test_backtrack_two(self): code = [ decode(line) for line in [ "inp w", "inp w", "inp x", "inp x", "inp x", "eql w x", "eql x 2", "add z w", "add z x", "eql z 2", "eql z 0" ] ] max_n = reduce_bounds(code, [9, 9, 9, 9, 9, 9, 9]) self.assertListEqual(max_n, [9, 2, 9, 9, 2, 9, 9])
def test_simple_0_set(self): code = [decode(line) for line in ["mul x 0", "add x 3", "eql z x"]] registers = track_dependencies(code) self.assertListEqual(registers.reg_w.inputs, []) self.assertListEqual(registers.reg_x.inputs, []) self.assertListEqual(registers.reg_y.inputs, []) self.assertEqual(registers.reg_x.values.min, 3) self.assertEqual(registers.reg_x.values.max, 3) self.assertListEqual(registers.reg_z.inputs, [])
def test_two_registers_single_value(self): code = [ decode(line) for line in ["inp z", "inp x", "mul z 3", "mul x 0", "eql z x"] ] registers = track_dependencies(code) self.assertListEqual(registers.reg_w.inputs, []) self.assertListEqual(registers.reg_x.inputs, []) self.assertListEqual(registers.reg_y.inputs, []) self.assertListEqual(registers.reg_z.inputs, [])
def test_two_registers(self): code = [ decode(line) for line in ["inp z", "inp x", "mul z 3", "eql z x"] ] registers = track_dependencies(code) self.assertListEqual(registers.reg_w.inputs, []) self.assertListEqual(registers.reg_x.inputs, [1]) self.assertListEqual(registers.reg_y.inputs, []) self.assertListEqual(registers.reg_z.inputs, [0, 1]) self.assertEqual(registers.reg_x.values.min, 1) self.assertEqual(registers.reg_x.values.max, 9) self.assertEqual(registers.reg_z.values.min, 0) self.assertEqual(registers.reg_z.values.max, 1)
def test_mod_zero(self): instruction = decode("mod z z") self.assertEqual(instruction.type, InstructionType.SET) self.assertEqual(instruction.source_register, None) self.assertEqual(instruction.source_value, 0) self.assertEqual(instruction.target_register, "z")
def test_div_one(self): instruction = decode("div z z") self.assertEqual(instruction.type, InstructionType.SET) self.assertEqual(instruction.source_register, None) self.assertEqual(instruction.source_value, 1) self.assertEqual(instruction.target_register, "z")