Here's an advanced version of the Count the Liberties challenge.
The definitions of the terms liberty and group are the same as the previous challenge, so have a look at the previous one for details, but briefly put,
- A group is a group of stones that are connected horizontally or vertically.
- Liberty is the number of empty spaces connected horizontally or vertically to a group.
For example,
. . O .
. X X .
. X . .
. O O .
black's group (X
) has 5 liberties, the upper group of white (O
) has 2 liberties, and the lower group of white has 3 liberties.
For input, you will be given an 2D array of arbitrary size, in which each cell has one of black
, white
, or empty
. You may map any value of any data type for black
, white
, and empty
; but 1 value can be mapped to each.
All groups in the input will always have 1 or more liberties.
For output, the cells that had empty
will be 0
, and the cells that had black
or white
will be filled with the number of liberties of its group.
Examples
. . O . 0 0 2 0
. X X . -> 0 5 5 0
. X . . 0 5 0 0
. O O . 0 3 3 0
. X . O -> 0 2 0 1
X 1
X 1
. -> 0
. 0
O 2
. 0
. X X . 0 3 3 0
X . X O -> 2 0 3 2
X X O . 2 2 1 0
If you have participated in the previous challenge, having to count the liberties of multiple groups may require a quite different strategy.