• + 0 comments

    Decided to get the coordinates of all possible crosses, then get the area for each combination of 2 that don't have overlapping coordinates, and finally return the maximum of those areas.

    Got stuck for a while until I remembered I was storing a container within another container so changing the position also cleared it in the list. Worked perfectly after importing 'copy.deepcopy()'.

    Python 3

    import itertools
    from copy import deepcopy
    def twoPluses(grid):
        count = "".join(grid).count("G")
        #If there is one or less 'G', then the product will be 0
        if count < 2:
            return 0
        #Need at least a 3x3 grid along with six 'G's to have a cross as an option
        if n == 2 or m == 2 or count < 6:
            return 1
        pos_list = []
        #Get the maximum of every possible 'G' cross in the grid
        pos = set()
        for y in range(n):
            for x in range(m):
                if grid[y][x] == "G":
                    size = 1
                    pos.add((x,y))
                    pos_list.append(deepcopy(pos))
                    while all([x-size>=0, x+size<m, y-size>=0, y+size<n]):
                        current_pos = [(x-size,y), (x+size,y), (x,y-size), (x,y+size)]
                        if "B" in map(lambda x: grid[x[1]][x[0]], current_pos):
                            break
                        size += 1
                        pos.update(current_pos)
                        pos_list.append(deepcopy(pos))
                    pos.clear()
        #Filter out the combinations that have overlapping squares
        combos = filter(lambda x: not bool(x[0]&x[1]), itertools.combinations(pos_list, 2))
        #Get the area of each remaining combination
        areas = map(lambda x: len(x[0])*len(x[1]), combos)
        #Return the maximum of the areas
        return max(areas)