So, in 2015, the Prime Minister of Singapore, Hsien Loong Lee, posted a C language sudoku solver to leetcode.com and I can see a lot of bitwise manipulation in the code. I think it is worth our time to take the solution to pieces and see how it runs so fast.
Sudoku Puzzles
Sudoku, also known as Su Doku, popular form of number game. In its simplest and most common configuration, sudoku consists of a 9 Γ 9 grid with numbers appearing in some of the squares. The object of the puzzle is to fill the remaining squares, using all the numbers 1β9 exactly once in each row, column, and the nine 3 Γ 3 subgrids. Sudoku is based entirely on logic, without any arithmetic involved, and the level of difficulty is determined by the quantity and positions of the original numbers. The puzzle, however, raised interesting combinatorial problems for mathematicians, two of whom proved in 2005 that there are 6,670,903,752,021,072,936,960 possible sudoku grids.
Although sudoku-type patterns had been used earlier in agricultural design, their first appearance in puzzle form was in 1979 in a New York-based puzzle magazine, which called them Number Place puzzles. They next appeared in 1984 in a magazine in Japan, where they acquired the name sudoku (abbreviated from “suuji wa dokushin ni kagiru”, meaning “the numbers must remain single”). In spite of the puzzleβs popularity in Japan, the worldwide sudoku explosion had to wait another 20 years.
Original Post
I found this headline posted on Hacker News although, fair warning, most of the comment thread is political about Singapore, Hsien Loong Lee, his background in Mathematics at Cambridge University, and other non-technical opinionated guff. I recommend you skip it.
I feel we are better off looking at the code, and seeing if we can reverse the technique to look for clever things
The original solution on leetcode.com shows that it passes all 6/6 test cases in 1ms, which is very impressive. There is a bitly link to the original code, and a MIT license making the code open source for anyone to use for any purpose.
It is MIT licensed
The C code comes with a bitly link to a Google Folder that contains the following License.txt
file:
The MIT License (MIT)
Copyright (c) 2015 Lee Hsien Loong
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
The C code example Sudoku Solver
The folder also contains the following Sudoku2.cpp
C code, below is a copy of the sudoku solver C code from the folder, lets take a look:
|
|
So, just reading through I can see some obvious stuff, some more interesting things that will need a few moments to understand, and at least two opportunities to optimize the code.
First run of the code
If you didn’t yet setup VS Code for C code debugging, go ahead and do that now.
So, the entry point of the code is the main()
function, as with any other C code. Let’s take a look at what it does.
|
|
We have a standard double-loop for a two dimensional array in variables i
and j
, and we can see that it is initializing the global integer arrays int InBlock[81], InRow[81], InCol[81];
. We should not for completeness that this is C code, so the arrays are all zero indexed, meaning that the indices i,j
range from 0..8
and the counter Square
goes from 0..80
. The interesting looking part of this code simply sets up a block indexer:
InBlock[] = 111 222 333
111 222 333
111 222 333
444 555 666
444 555 666
444 555 666
777 888 999
777 888 999
777 888 999
Further, we have some single dimensional arrays being initialized: int Sequence[81], Entry[81], LevelCount[81];
. Also, we initialize the Block[], Row[] and Col[]
arrays.
So indeed for the first run, set a breakpoint at ConsoleInput();
in main()
and let’s check that those arrays are initialized as we think they will be.
Okay, so the next thing the code does is receive input on the command line so that the user can set up the puzzle array.
But, we don’t want to have to type all of that in every time we run the code, so instead, we will force the values into the array. To make this work, we will add a global flag called TestMode
and when it is set, we will ignore the console input and instead load the test values.
int TestMode = 1;
...
void TestInput()
{
// 1-- 47- ---
// --- 162 7-4
// -6- --- ---
// 871 -45 9-6
// 3-- --- -51
// 256 -9- -7-
// -27 -1- 5-8
// -15 68- -42
// 6-3 --- 1--
InitEntry(0, 0, 1);
InitEntry(0, 3, 4);
InitEntry(0, 4, 7);
InitEntry(1, 3, 1);
InitEntry(1, 4, 6);
InitEntry(1, 5, 2);
InitEntry(1, 6, 7);
InitEntry(1, 8, 4);
<---snip--->
}
...
if (TestMode == 0)
ConsoleInput();
else
TestInput();
Code Optimization
We can clean up the array initialization code with a modification to the globals:
int Sequence[81] = {0};
int LevelCount[81] = {0};
If, for some reason, you don’t trust C style array initialization then calling memset(Sequence, 0, 81); memset(LevelCount, 0, 81);
in main()
would do the same job. And can also be used in the main body of the code at run-time rather than just at initialization.
Now, we can get rid of the second loop in main()
and replace the initialization code with the following:
int i, j, Square;
for (i = 0; i < 9; i++)
for (j = 0; j < 9; j++)
{
Square = 9 * i + j;
InRow[Square] = i;
InCol[Square] = j;
InBlock[Square] = (i / 3) * 3 + (j / 3);
Sequence[Square] = Square;
}
for (i = 0; i < 9; i++)
Block[i] = Row[i] = Col[i] = ONES;
The function should make a very simple check to see if S1 == S2
and return before doing any swapping:
void SwapSeqEntries(int S1, int S2)
{
if (S1 == S2) return;
int temp = Sequence[S2];
Sequence[S2] = Sequence[S1];
Sequence[S1] = temp;
}
I find the PrintArray()
function to be quite clunky, personally not a fan of putc()
, but I stuck with it for now. I am surprised that the author didn’t know that the mathematical inverse of \(2^x\) is \(log_2(x)\), so I made that change (and had to #include "math.h"
for the function). The PrintArray()
function main loop now appears:
for (i = 0; i < 9; i++)
{
if (i % 3 == 0)
putc('\n', stdout);
for (j = 0; j < 9; j++)
{
if (j % 3 == 0)
putc(' ', stdout);
valbit = Entry[Square++];
if (valbit == 0)
ch = '-';
else
ch = '0' + log2(valbit);
putc(ch, stdout);
}
putc('\n', stdout);
}