import math


def fitness(chromosome):
    """Returns the fitness value of a chromosome.

    Fitness is is calculated by summing up all the occasions where a queen is
    under attack by some other queen (if three or more queens exists on the
    same row or diagonal, all of them are considered threatening each other).
    """
    fitness = 0
    for i in range(len(chromosome)):
        for j in range(i + 1, len(chromosome)):
            # Calculate the distances of chromosome[i] and chromosome[j]
            x_distance = math.fabs(j - i)
            y_distance = math.fabs(chromosome[j] - chromosome[i])
            # Increment fitness value if chromosome[i] and chromosome[j] are
            # on the same row or diagonal.
            fitness += y_distance == 0
            fitness += x_distance == y_distance
    return fitness