Beta

Binary Sudoku - Part I

Description:

Binary Sudoku is a little different to normal sudoku.

In Binary Sudoku, you're given a square grid, the sides of which are always an even number of squares, which is to be filled with ones and zeroes (ticks and crosses, X and O, whatever) according to the following rules:

  • There must be the same number of each symbol in each row and column.
  • There cannot be more than two of any symbol together in any row or column.
  • No row or column can be repeated.

Simple!

If the cells are filled in as ones and zeroes, it should be obvious that a puzzle of size n can be represented as a series of n-bit binary numbers which conform to the above rules.

If we consider the four-bit binary numbers, we can see which are valid for rows or columns in the puzzle and which are not. (At this stage, we're only looking at numbers being valid for use, not at combinations of numbers being valid or not.)

Dec Binary Valid?
 0   0000    No
 1   0001    No
 2   0010    No
 3   0011   Yes
 4   0100    No
 5   0101   Yes
 6   0110   Yes
 7   0111    No
 8   1000    No
 9   1001   Yes
10   1010   Yes
11   1011    No
12   1100   Yes
13   1101    No
14   1110    No
15   1111    No

In this first kata in the series, your job is simply to say whether a given decimal number represents a valid configuration for a row (or column) in a Takuzu puzzle of a given size.

To do this, we need to convert the decimal number to binary and then, if the binary representation has less bits than the size, add leading zeroes.

You can assume that no negative numbers will be given, and that the size will be less than or equal to 50.

Some examples:

valid_row? 92, 8 = false
The 8-bit binary representation of 92 is 0101 1100
0101 1100 contains 4 1 bits and 4 0 bits, so the first rule is passed
0101 1100 has three one bits in a row, so the second rule fails.

valid_row? 154, 8 = true
The 8-bit binary represenation of 154 is 1001 1010
1001 1010 contais 4 1 bits and 4 0 bits, so the first rule is passed
1001 1010 has no strings of three of the same bit value in a row, so the second rule is passed.

valid_row? 154, 10 = false
The 10-bit binary representation of 154 is 00 1001 1010
00 1001 1010 contains 4 1 bits and 6 0 bits, so the first rule fails
Algorithms

Similar Kata:

Stats:

CreatedApr 17, 2022
PublishedApr 17, 2022
Warriors Trained28
Total Skips6
Total Code Submissions88
Total Times Completed13
Ruby Completions13
Total Stars2
% of votes with a positive feedback rating78% of 9
Total "Very Satisfied" Votes5
Total "Somewhat Satisfied" Votes4
Total "Not Satisfied" Votes0
Total Rank Assessments9
Average Assessed Rank
7 kyu
Highest Assessed Rank
7 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • jebreen Avatar
Ad