1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def text = """\
□■■□
■□□□
□□□■
□■■□"""

def score = ["■":0, "□":0]

board = text.readLines().collect{ it.split("")[1..-1] }
XMAX = board.size() - 1
YMAX = board[0].size() - 1

def eatAround( mark, x, y ){
    [["x":x-1, "y":y], ["x":x+1, "y":y], ["x":x, "y":y-1], ["x":x, "y":y+1]].findAll{
        (it.x in 0..XMAX) && (it.y in 0..YMAX) && board[it.x][it.y] == mark
    }.each{
        board[it.x][it.y] = "×"
        eatAround( mark, it.x, it.y )
    }
}

(0..XMAX).each{ x ->
    (0..YMAX).each{ y ->
        def mark = board[x][y]
        if( mark != "×" ){
            score[mark]++
            eatAround(mark, x, y)
        }
    }
}

println score