Takes a 2d grid and figures out how many different elements are in it, so
    that we know how big to make the palette. Also avoids the unfortunate
    red/green palette that results from too few elements.

    Returns int indicating the length the palette should have.

def get_pallete_length(grid):
    """
    Takes a 2d grid and figures out how many different elements are in it, so
    that we know how big to make the palette. Also avoids the unfortunate
    red/green palette that results from too few elements.

    Returns int indicating the length the palette should have.
    """
    elements = list(set(flatten_array(grid)))
    length = len(elements)

    if type(elements[0]) is str:
        lengths = [len(el) for el in elements if not el.startswith("-")]
        if max(lengths) < 5:  # Mixing red and green
            length += 2  # is not pretty so let's avoid it
    return length