def test_part2(self): """Test Knots part two examples""" # 1. Loop for all of the part two examples for p2example in P2_EXAMPLES: # 2. Create the knot object myknot = knots.Knots(length=256, part2=True) # 3. Knot it up, and check result self.assertEqual(myknot.process_knots(text=p2example[0], verbose=False), p2example[1])
def test_value_init(self): """Test Knots creation with values""" # 1. Create Knots object from values myknot = knots.Knots(length=256) # 2. Make sure it has the specified values self.assertEqual(myknot.part2, False) self.assertEqual(myknot.length, 256) self.assertEqual(myknot.current, 0) self.assertEqual(myknot.skip, 0) self.assertEqual(len(myknot.values), 256)
def test_empty_init(self): """Test default Knots creation""" # 1. Create default Knots object myknot = knots.Knots() # 2. Make sure it has the default values self.assertEqual(myknot.part2, False) self.assertEqual(myknot.length, 0) self.assertEqual(myknot.current, 0) self.assertEqual(myknot.skip, 0) self.assertEqual(myknot.values, []) self.assertEqual(myknot.rounds, 1) self.assertEqual(myknot.knots, [])
def part_two(args, input_lines): "Process part two of the puzzle" # 1. Create the puzzle solver solver = knots.Knots(part2=True, length=256) # 2. know up the string and get the product of the first two values solution = solver.process_knots(text=input_lines[0], verbose=args.verbose, limit=args.limit) if solution is None: print("There is no solution") else: print("The dense hash is %s" % (solution)) # 3. Return result return solution is not None
def test_example(self): """Test Knots example""" # 1. Create Knots object from values myknot = knots.Knots(length=5) # 2. Make sure it has the specified values self.assertEqual(myknot.part2, False) self.assertEqual(myknot.length, 5) self.assertEqual(myknot.current, P1_CURRENT[0]) self.assertEqual(myknot.skip, P1_SKIP[0]) self.assertEqual(myknot.values, P1_VALUES[0]) # 3. Process the first knot self.assertEqual(myknot.process_knots(text=P1_EXAMPLE), P1_RESULT) self.assertEqual(myknot.current, P1_CURRENT[4]) self.assertEqual(myknot.skip, P1_SKIP[4]) self.assertEqual(myknot.values, P1_VALUES[4])
def test_part_one_example_bits(self): """Test Disk Hash Knot bits from example""" # 1. Loop for the eight lines of the example for lnum, line in enumerate(aoc_14.from_text(EXAMPLE_HASH_KNOT_BITS)): if line[0] == '|' or line[0] == 'V': continue # 2. Create the knot object myknot = knots.Knots(length=256, part2=True) # 3. Generate the hash key hash_key = EXAMPLE_HASH_KNOT_KEY + '-%d' % lnum # 4. Get the dense hash dense_hash = myknot.process_knots(text=hash_key, verbose=False) # 5. Convert the first two digits to bits hash_bits = EXAMPLE_HASH_KNOT_HEX[dense_hash[0]] + \ EXAMPLE_HASH_KNOT_HEX[dense_hash[1]] # 6. Compare with example self.assertEqual(hash_bits, line[0:8])
def populate_bits(self): "Populate the bits from the knot hash" # 0. Precondition axiom assert self.size <= 128 # 1. loop for each row of bits for rnum in range(self.size): # 2. Generate row key row_key = self.text + '-%d' % rnum # 3. Get the dense hash knot = knots.Knots(length=256, part2=True) # 4. Get the dense hash dense_hash = knot.process_knots(text=row_key, verbose=False) # 5. Convert the dense_hash to bits bits = self.hash_to_bits(dense_hash) # 6. Save the bits self.bits[rnum] = bits
def test_example_step_by_step(self): """Test Knots example knot by knot""" # 1. Create Knots object from values myknot = knots.Knots(length=5) # 2. Make sure it has the specified values self.assertEqual(myknot.part2, False) self.assertEqual(myknot.length, 5) self.assertEqual(myknot.current, P1_CURRENT[0]) self.assertEqual(myknot.skip, P1_SKIP[0]) self.assertEqual(myknot.values, P1_VALUES[0]) # 3. Process the first knot myknot.process_one_knot(3, verbose=False) self.assertEqual(myknot.current, P1_CURRENT[1]) self.assertEqual(myknot.skip, P1_SKIP[1]) self.assertEqual(myknot.values, P1_VALUES[1]) # 4. Process the second knot myknot.process_one_knot(4, verbose=False) self.assertEqual(myknot.current, P1_CURRENT[2]) self.assertEqual(myknot.skip, P1_SKIP[2]) self.assertEqual(myknot.values, P1_VALUES[2]) # 5. Process the third knot myknot.process_one_knot(1, verbose=False) self.assertEqual(myknot.current, P1_CURRENT[3]) self.assertEqual(myknot.skip, P1_SKIP[3]) self.assertEqual(myknot.values, P1_VALUES[3]) # 6. Process the fourth knot myknot.process_one_knot(5, verbose=False) self.assertEqual(myknot.current, P1_CURRENT[4]) self.assertEqual(myknot.skip, P1_SKIP[4]) self.assertEqual(myknot.values, P1_VALUES[4])