I recently found this game and have been enjoying it. Basically, there is a grid of colored tiles and you act by clicking on empty spaces. When you click an empty space, the first tile (if any) in each of the four axis-aligned directions is selected. If any color appears more than once, all tiles of that color are destroyed (and if it hits two of one color and two of another, all four are destroyed).
See the following example:
. . R . . . . . . . . . . . . R---x G . . . . G . . . . . . . . | . . => . . . | . => . . . . . . G | . R . G---x R . . . . R . . R . . . . . . . . . . . . The game is played on a time limit and clicking a space that does nothing will incur a time penalty, but for this challenge, I am just interested in one thing: can I clear the entire grid?
For example, in this grid, two clicks are enough to clear the grid:
. R . . . B . B . R . . Clicking between the Bs opens a space between the Rs that can be clicked to finish it.
On the other hand, in this grid, there is no way to eliminate all tiles:
G . G . G If you click between the right two Gs, then you can't eliminate the left one, and same for the other direction.
Given a grid represented as any reasonable format (for example, a matrix of non-negative integers where spaces are 0 is what I predict will be most common), output whether or not the grid can be cleared (refer to the consensus for output format).
You may assume there will be no more than 9 colors; this is so that input can be taken as a list of strings of digits.
Here are some example test cases. The following are all clearable:
. R . G B . B . . R . G [[0, 1, 0, 2], [3, 0, 3, 0], [0, 1, 0, 2]] . R G . R . . G . R G . [[0, 1, 2, 0], [1, 0, 0, 2], [0, 1, 2, 0]] . R . G R . G R . R . . [[0, 1, 0, 2], [1, 0, 2, 1], [0, 1, 0, 0]] R G . G G R . . . . R G G . G R [[1, 2, 0, 2], [2, 1, 0, 0], [0, 0, 1, 2], [2, 0, 2, 1]] The following are not:
. R G . G . . R G . . R . R G . [[0, 1, 2, 0], [2, 0, 0, 1], [2, 0, 0, 1], [0, 1, 2, 0]] R . R . R [[1, 0, 1, 0, 1]] R . . . . R . R . [[1, 0, 0], [0, 0, 1], [0, 1, 0]] R G R G [[1, 2, 1, 2]] . R B . R . G R . G B . [[0, 1, 3, 0], [1, 0, 2, 1], [0, 2, 3, 0]] You may assume that the board contains at least one cell, but either [[0]] or [[1]] are valid test cases.
This is a code-golf challenge, so the shortest submission (in bytes) for each language is the winner!
[[0, 1, 3, 0], [1, 0, 2, 1], [0, 2, 3, 0]] -> False\$\endgroup\$