def __init__(self, size, blocks): self._size = size self.blocks = blocks for block in self.blocks: location = block.location for i in range(len(block)): cell = block[location] if cell is not None: continue is_service, values = Kakuro._get_content(block, location) adjacent_block = self.find_adjacent_block(block, location) if adjacent_block is not None: is_service_abj, values_adj = Kakuro._get_content( adjacent_block, location, cell) if not (is_service ^ is_service_abj): new_cell = Cell(is_service, values + values_adj, location) else: raise ValueError("Something went wrong") adjacent_block[location] = new_cell else: new_cell = Cell(is_service, values, location) block[location] = new_cell location = location + (Location(1, 0) if block.is_horizontal else Location(0, 1))
def test_find_adjacent_block(self): block = self.kakuro.blocks[0] expected = [Location(0, 1), Location(0, 2)] actual = [ x.location for x, y in self.kakuro.find_all_adjacents_blocks(block) ] self.assertSequenceEqual(expected, actual)
def find_adjacent_block(self, block, location): for adjacent_block in self.blocks: if adjacent_block == block or block.is_horizontal == adjacent_block.is_horizontal: continue current_location = location while current_location.x >= 0 and current_location.y >= 0: if adjacent_block.location == current_location: if current_location.distance_on_line(location) < len( adjacent_block): return adjacent_block current_location = current_location + Location( -1, 0) if not block.is_horizontal else Location(0, -1)
def test_get_known_values(self): locations =[Location(0, 2), Location(0, 3), Location(0, 4)] values = [2, 4, 6] for i in range(len(values)): self.block[locations[i]] = Cell(False, values[i], locations[i]) self.assertSequenceEqual(values, self.block.get_known_values())
def setUp(self): self.block = Block(Location(0, 1), 12, 4, True) location = Location(0, 1) self.block[location] = Cell(True, 12, location)
def test_equals_get_block_partitions_and_get_partitions(self): block = Block(Location(1, 0), 16, 3, False) self.assertSequenceEqual(['97'], Solver.get_block_partitions(block))