import itertools


def _energy(sudoku, indexer):
    """
    Calculates initial energy distribution.

    Arguments:
        sudoku: np.array
            A sudoku array with all cells filled most likely incorrectly.
        indices: list(Index)
            Essential indices for a sudoku array.

    Returns:
        energy: int
            Energy of a system with a solved case corresponding to zero.
    """
    energy = 0
    num_rows, num_cols = sudoku.shape
    for coord in itertools.product(range(num_rows), range(num_cols)):
        for slices in indexer.crosses(*coord):
            energy += 9 - len(set(sudoku[slices].flatten()))

    assert energy % 9 == 0, "Invalid calcultion."

    # normalize for double counts
    energy //= 9

    energy += sum(9 - len(set(sudoku[block].flatten())) for block in indexer.blocks)

    return energy