Takuzu is a logic game in which you have to complete a grid with cells containing 0
s and 1
s. The grid must follow 3 rules:
- No three horizontal or vertical consecutive cells can be the same.
- There must be an equal number of
0
s and1
s in each row and column. - No two rows can be the same, and no two columns can be the same.
Let's look at a finished grid:
0011
1100
0101
1010
As you can see, this board follows rule 1
, 2
and 3
. There are no three horizontal or vertical cells that are the same, all the rows and columns contain an equal number of 0
s and 1
s, and no two rows and no two columns are the same.
Let's look at a grid that isn't valid:
110100
010011
011010
101100
100011
001101
There's a bunch of problems with this grid. For example, row 5
has three 0
s in a row, and column 2
has three 1
s in a row, followed by three 0
s. Therefore, this is not a valid grid.
Task:
Your task is to make a program which, given a 2D array of n
* n
0
s and 1
s, verifies the board to see if it's a valid, finished Takuzu board.
Examples:
0011
1100
0101
1010
This board follows all the rules, and is therefore a valid Takuzu board. You must return a truthy value for this.
11
00
This is not a valid board - row 1
doesn't follow rule 2
. You must return a falsey value for this.
100110
101001
010101
100110
011010
011001
This is not a valid board, it fails (only) due to rule 3 - the first and fourth rows are the same.
110100
001011
010011
101100
100110
011001
This is not a valid board, it fails (only) due to rule 3 - the first and fourth columns are the same.
011010
010101
101100
010011
100110
101001
This is a valid board.
Rules and Specs:
- You can assume that all boards are square of dimensions
n * n
, wheren
is a positive even integer. - You can assume that all boards are finished.
- You may take input as a 2D array containing values signifying
0
and1
, or as a string. - You must output consistent truthy and falsey values for truthy and falsey boards, and the values representing "truthy" and "falsey" cannot be the same.
This is code-golf, so shortest code in bytes wins!