• + 0 comments

    Ruby

    def takeLargestCruiseCoord(i,j, grid)
        layersCruise = []
        if grid[i][j] == 'G'
            index = 1
            layersCruise << [i,j]
            loop do
                layer = [[i+index,j], [i-index,j],[i,j+index],[i,j-index]]
                layer = layer.select{ |i,j| i >=0 and i < grid.length and j >= 0 and j < grid[0].length }
                layer = layer.select{ |i,j| grid[i][j] == 'G'}
                layersCruise += layer if layer.length == 4
                index += 1
                break if layer.length != 4
            end
        end
        layersCruise
    end
    
    def twoPluses(grid)
        all_coords = []
        grid.each_index { |i| grid[0].chars.each_index { |j| all_coords << [i,j] } } 
        all_coords_comb = all_coords.combination(2).to_a
        max_value = 0
        all_coords_comb.each do |pair_coord|
            coord1_i, coord1_j = pair_coord[0]
            coord2_i, coord2_j = pair_coord[1]
            next if grid[coord1_i][coord1_j] == 'B' or grid[coord2_i][coord2_j] == 'B'
            largest_cruise_1 = takeLargestCruiseCoord(coord1_i, coord1_j, grid)
            while largest_cruise_1.length >= 1 do
                grid_cpy = grid.map(&:dup)
                largest_cruise_1.each { |i,j| grid_cpy[i][j] = 'B' }
                second_largest_cruise = takeLargestCruiseCoord(coord2_i, coord2_j, grid_cpy)
                counter = largest_cruise_1.length * second_largest_cruise.length
                max_value = counter if counter > max_value
                largest_cruise_1.pop(4)              
            end
        end
        max_value
    end