10
1
Introduction
Rules of the puzzle:
The puzzle Binary (also known as Takuzu or Subiku) is very simple to understand, and has only a few rules:
Since the name of the game is binary it's pretty obvious, but you can only fill in zeros and ones.
- No more than two of the same digit can be vertically or horizontally adjacent to each other
- Each row and each column must contain an equal amount of zeros and ones (this implicitly means every binary game will always have even dimensions).
- There may be no duplicated rows and no duplicated columns (with the exact same order of zeros and ones).
You can play the game at www.binarypuzzle.com if you want to.
Tactics:
Due to rule 1, we can always fill in a digit if:
- There are already two of the same digit vertically or horizontally adjacent to each other, in which case we can fill in the opposite digit at both sides. I.e. .11...
→ 0110..
.
- There are two of the same digit vertically or horizontally with only one gap in between them. I.e. .1.1..
→ .101..
Due to rule 1, when three gaps are left and we cannot have three adjacent of the same digit, we can fill in one of the gaps. I.e. .0.1.0
→ 10.1.0
(We still have to fill in two ones, and we cannot have three adjacent ones in the middle, so the first gap has to be a 1
.)
Due to rule 2, we can always fill in the remaining gaps in a row or column if halve of them is already filled in with the opposite digit. I.e. .1.011
→ 010011
Due to rule 3, we can always fill in the opposite digits if only two are left to solve on an equally ordered line. I.e. 101100 & 1..100
→ 101100 & 110100
Due to rule 3, we can sometimes fill in a gap when three gaps are left on an equally ordered line. I.e. 010011 & .1.01.
→ 010011 & .1.010
(Here we cannot fill in a 1
at the end, because that would mean we have to fill in zeros at the other two gaps, making both lines equal in order.)
Example:
We start with the following 6x6 grid with some ones and zeros filled in (and the dots are gaps we have yet to fill in):
.1....
.10.0.
1.11..
.1....
...1.0
......
Due to rules 1 & 2 we can fill in these digits:
.1.01.
.1010.
101100
010011
.0.1.0
.010..
Due to rule 1 we can fill in a 1 at row 5, column 1:
.1.01.
.1010.
101100
010011
10.1.0
.010..
Due to rule 3 we can fill in a 0 at row 1, column 6 (when looking at row 4):
.1.010
.1010.
101100
010011
10.1.0
.010..
Now we can continue to fill gaps with digits due to rules 1 & 2:
.1.010
010101
101100
010011
10.1.0
.010.1
Now we can finish row 5 due to rule 3 (when looking at row 3):
.1.010
010101
101100
010011
100110
.010.1
And then we can finish the puzzle due to rules 1 & 2:
011010
010101
101100
010011
100110
101001
Challenge:
The challenge is simply: given the starting grid, output the solved puzzle.
NOTE: You don't have to implement the rules above. You of course can, and it should give you hints on how to implement this challenge, but bruteforcing the solution with the rules in mind is completely fine.
How you solve it is up to you, but the challenge is to output the solved puzzle.
Challenge rules:
- Input and output format for the grid is flexible, but please state what you use. (I.e. 2D byte-array; String with newlines; etc.)
- This above also applies to the characters used. In the example I've used
01.
, but if you want you can useABx
instead. Please state what input/output format and characters you've used. - You can assume only the following grid sizes will be used:
6x6
;8x8
;10x10
;12x12
;14x14
;16x16
.
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language. - Standard rules apply for your answer, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code.
- Also, please add an explanation if necessary.
Test cases:
The dots are only added for readability, feel free to use spaces or anything else you prefer for gaps instead. Both in and output format is flexible.
Input:
1..0..
..00.1
.00..1
......
00.1..
.1..00
Output:
101010
010011
100101
011010
001101
110100
Input:
.1....
.10.0.
1.11..
.1....
...1.0
......
Output:
011010
010101
101100
010011
100110
101001
Input:
.......1..
.00..0..1.
.0..1..0.0
..1...1...
1.1......1
.......1..
.0..1...0.
....11...0
.0.0..1..0
0...0...1.
Output:
0110010101
1001100110
1001101010
0110011001
1010100101
0101010110
1001101001
0110110100
1010011010
0101001011
It's been in the sandbox since April 4th. - Related: A 4x4 Challenge – Kevin Cruijssen – 2017-05-04T12:26:05.700
1Related OEIS entry. – Arnauld – 2017-05-04T15:33:06.140