Background
This challenge is about the game of Go. Go is a game played on a board with a square grid N x N
. You don't have to know how to play the game to do this challenge, but if you are interested, google "Tromp-Taylor rules" for a minimal and precise set of rules to play a full game.
Let's use a 4 x 4
board. As the game starts, two players, black (X
) and white (O
), alternately place a stone on an empty grid starting with black.
At some point of the game, the board may look like this.
. . O .
. X X .
. X . .
. O O .
Black has 1 group, and white has 2 groups. A group is a group of stones that are connected horizontally or vertically.
. X X .
X . X .
X X . .
. . X X
Black has 3 groups on this board.
. . O .
. X X .
. X . .
. O O .
Back to the first example, the upper group of white has 2 liberties and the lower group of white has 3 liberties. Liberty is the number of empty spaces connected horizontally or vertically to a group.
X . O X
. . . .
. . . O
. . O X
There are 3 black groups on this board each with 2, 1, and 0 liberties. In an actual game a group with 0 liberties are taken out of the board, but you don't have to care about that in this challenge.
Challenge
The input is a 4 x 4
Go board position, where there is 1 black group and any number of white groups.
The output is the number of liberties that the black group has.
The input can be encoded in any way that can hold \$3^{4\times4}\$ distinct values.
The output is an integer, when optionally printed, up to base 16.
Examples
. . . .
. X . .
. . . .
. . . . -> 4
. . . .
. X X .
. . . .
. . . . -> 6
X X . .
X . . .
. . . .
. . . . -> 3
X X O .
X O . .
O . . .
. . . . -> 0
. X X .
. X . X
. X X X
. . . . -> 8
O X X .
O X . X
O X X X
. O O O -> 2
O X X O
O X O X
O X X X
. O O O -> 0
The last case is an impossible position in the actual game, but it is valid for this challenge.