05AB1E, 85 bytes
2FINi(}Θ©˜!ƶIgä®*Δ2Fø0δ.ø}2Fø€ü3}®*εεÅsyøÅs«à]+ÐU˜ê©¦vyX®¦y†AS:¾1:'a0:4FJTx:€Søí}S2¢: "If you have participated in the previous challenge, having to count the liberties of multiple groups may require a quite different strategy." That's an understatement, haha. This answer is almost 8 times as large as my 05AB1E answer for the related challenge. Although this is primarily because flood-filling is so expensive in 05AB1E; and the conversion I use is currently pretty verbose as well..
Uses -1/1 for O/X and 0 for empty spaces.
Try it online or verify all test cases.
Explanation:
TODO: Will add an explanation later today. I'm first going to lunch and have a meeting at work.
As a short summary for now:
- Flood-fill the individual islands, which I've done in 05AB1E before for this challenge (which actually contained a bug that I discovered thanks to the final test case here);
- Loop over the islands:
- Convert the matrix to something usable, so a similar approach can be used as in the linked challenge (converts
0s to1s; the current island to0s; and the other islands to lowercase letters). - Actually use that approach to calculate the counts of each island
- Replace the islands with these counts.
- Convert the matrix to something usable, so a similar approach can be used as in the linked challenge (converts