The following 101 bytes K program can solve Sudoku puzzles:
Here is an example of using the solver to solve this puzzle:
q sudoku.k 200370009009200007001004002050000800008000900006000040900100500800007600400089001 2>/dev/null </dev/null
And the result:
284375169639218457571964382152496873348752916796831245967143528813527694425689731
See below for more information about this program, and for solvers in other languages.
This Sudoku solver in C was written by Oli Charlesworth and reduced by Joel Hockey and is 174 bytes long:
To compile and then run (using GCC for this example):
gcc -o sudoku sudoku.c && ./sudoku 200370009009200007001004002050000800008000900006000040900100500800007600400089001
The program also compiles and runs successfully in Microsoft Visual Studio.
Oli also pointed out that this program is not strictly legal C, and suggested another slightly longer version (198 bytes) that is legal C.
Contributors:
The first C version was 340 bytes, provided by David Barr.
Oli Charlesworth then sent a 272 bytes version, and later reduced it to 190, and then 176 bytes.
Joel Hockey knocked another 2 bytes off.
A Groovy Sudoku Solver in 208 bytes.
Contributors:
Paul King wrote the first version in 247 bytes, based on the Ruby code.
Members of the Groovy user mailing list helped reduce the code to 186 bytes.
Sabine te Spenke corrected an error when the board starts with a 0, adding 22 bytes to the length.
To run:
ghc --make sudoku.hs && a.out < sudoku.txt
Contributors:
flagitious provided the first version at 244 bytes.
The shortest Java Sudoku solver is 258 bytes.
Here is an example of how to run the program:
javac S.java; java S 200370009009200007001004002050000800008000900006000040900100500800007600400089001
Contributors:
David Singleton created the algorithm and did the initial reduction work.
Mick Killianey and David Beaumont reduced it to 299 bytes.
Roy van Rijn reduced it to 297 bytes.
Mick Killianey and David Beaumont reduced it further to 295 bytes.
David Singleton and Mick Killianey knocked one more byte off (294 bytes).
Joel Hockey rewrote the algorithm and used varargs to make main() the recursive function to knock off 5 more (286 bytes).
Joel Hockey knocked another 3 bytes off.
Whoaa! Joel Hockey knocked another 25 bytes off.
This Sudoku solver written in K is 101 bytes long.
K is a general purpose language produced by kx. It is used mainly in the financial community. Some language documentation is available.
If we allow printing spaces between the characters, the program can be shortened to 95 bytes:
p:+{(=x)x}'p,,3/:_(p:,/'+:\9#'!9)%3 *{$[&/x;,x;,/.z.s'@[x;i;:;]'&27=x[,/p i:x?0]?!10]}@.:'*.z.x
This program is run as follow:
$ q sudoku.k 200370009009200007001004002050000800008000900006000040900100500800007600400089001 2>/dev/null </dev/null
And here is the output:
2 8 4 3 7 5 1 6 9 6 3 9 2 1 8 4 5 7 5 7 1 9 6 4 3 8 2 1 5 2 4 9 6 8 7 3 3 4 8 7 5 2 9 1 6 7 9 6 8 3 1 2 4 5 9 6 7 1 4 3 5 2 8 8 1 3 5 2 7 6 9 4 4 2 5 6 8 9 7 3 1
Contributors:
Originally written by Arthur Whitney (the developer of K) and Attila Vrabecz.
Small changes made by Jason Nordwick.
Members of the K mailing list also suggested changes.
The following Object Caml program is 239 bytes.
Example usage:
echo 000010000301400860900500200700160000020805010000097004003004006048006907000080000 | ocaml sudoku.ml
An Objective Caml interpreter is available for free download.
Contributors:
First version written by Tim Lopez (253 bytes), based on the Perl version.
Mark Byers changed && to &, reducing it to 249 bytes.
Shinh changed _ let _= to ;;, reducing it to 244 bytes.
2007-06-02: Shinh made another small change. 239 bytes.
The following Perl program that can solve Sudoku is 121 bytes:
Example usage:
echo 000010000301400860900500200700160000020805010000097004003004006048006907000080000 | perl sudoku.pl
The output is:
452618379371429865986573241734162598629845713815397624293754186148236957567981432
Notes:
The program still works if ${…}for is changed to map{…} , saving one byte. However now the program can consume large amounts of memory:
$_=$`.$_.$'.<>;split//;map{/[@_[map{$i-($i="@-")%9+$_,9*$_+ $i%9,9*$_%26+$i-$i%27+$i%9-$i%3}0..8]]/o||do$0}/0/||print..9
Contributors:
This program is a slightly modified version of another three line Sudoku Solver (page includes a description of how it works).
Mark Byers reduced it to 187 bytes.
Gordon McCreight reduced it to 186 bytes.
Pablo Carbonell reduced it to 181 bytes, and provided this comment:
The difference with the original perl algorithm is that in this version I am taking in every iteration a slice @B with the values of the contiguous cells. The trickiest thing, I think, is the way this slice is used as a regular expression range in the grep.
Simon Stroh changed @A=split//,<> to $/=\1;@A=<> to reduce the program to 179 bytes.
Mitsuru Kariya changed @A[map{ … }] to map@A[ … ] to reduce the program to 178 bytes.
Ton Hospel shortened the program to 121 bytes.
The current shortest Python sudoku solver program is 169 bytes long. This program is clean, in that it does not use any unusual features such as outputting to standard error, or crashing instead of terminating properly. It lists all solutions if there are more than one. It requires 6 lines each with no more than 80 characters.
To run the program, use the following syntax:
echo 000010000301400860900500200700160000020805010000097004003004006048006907000080000 | python sudoku.py
It is also possible to write a program that fits on 3 lines with no line longer than 80 characters, but now 178 bytes are required:
def r(a):i=a.find('0');~i or exit(a);[m in[(i-j)%9*(i/9^j/9)*(i/27^j/27|i%9 /3^j%9/3)or a[j]for j in range(81)]or r(a[:i]+m+a[i+1:])for m in`14**7*9`] from sys import*;r(argv[1])
This is the dirtiest version, where we allow ourselves to cheat a little to get the byte count down. It displays what it is thinking so that you can see how the program works. The program terminates with an exception, which must be redirected and the command line required to start the program is slightly more complicated than for the other programs. The solution is displayed on the last line of standard out. This program is 155 bytes.
def r(a):print a;i=a.index('0');[m in[(i-j)%9*(i/9^j/9)*(i/27^j/27|i%9/3^j%9/3)or a[j]for j in range(81)]or r(a[:i]+m+a[i+1:])for m in`14**7*9`] r(input())
To run the program, use the following command line:
echo \'000010000301400860900500200700160000020805010000097004003004006048006907000080000\' | python sudoku.py 2>/dev/null
Contributors:
The original python program of length 178 bytes was written by Mark Byers, with help from marienz and Random832, originally based on the Perl program above.
David Warren changed ‘%d’%5**18 to `14**7*9` which reduced the program to 177 bytes (and also improves the performance of the algorithm).
Yu-Xi Lim suggested using raw_input() and input() which reduced the clean version to 169 bytes and the ‘cheating’ version to 155 bytes.
The current shortest Ruby Sudoku solver program is 122 bytes long.
This program prints all the solutions if there are multiple solutions (a real Sudoku should only have one solution).
To run this Ruby program use the following syntax:
$ ruby sudoku.rb 200370009009200007001004002050000800008000900006000040900100500800007600400089001
The program can be made a further one byte shorter if the 80 character per line restriction is removed, by putting everything on one long line.
The quotes around the result can be removed, but this adds 1 more byte to the length of the program:
$*.map{|a|(i=a=~/0/)?(v=*?1..?9).fill{|j|v-=[a[j+i-k=i%9],a[ k+j*=9],a[j%26+i-i%3-i%27+k]]}+v.map{|k|$*.<<$`<<k<<$'}:$><<a}
There is also an explanation of how an earlier version of this program works here: ShortestSudokuSolverRuby.
Contributors:
The program is a port of the Perl version to Ruby initially by Justin Giancola.
Mark Byers reduced it to 134 bytes.
acw1668 changed (p a) to p(a) to reduce it to 133 bytes, saving one byte.
Martin DeMello changed (?1..?9).map to [*?1..?9] to reduce the program to 130 bytes, saving three bytes.
flagitious reduced the program to 122 bytes (with thanks to marmaladea for the trick of changing $*<<(…) to $*.<<… .
Programs should not contain lines longer than 80 characters. If the language allows splitting a long line into two shorter lines, using this feature is acceptable.
For compiled languages, source code size is counted, not compiled size.
New line characters are counted as one byte.
A valid program can accept any 9x9 Sudoku from standard input (or else the first command line parameter, if you prefer) and should output the result to standard out. I will also list some of the programs that cheat very slightly (for example, by outputing to standard error instead of standard out).
If command line parameters are required, they are counted as extra bytes using the perlgolf counting rules.
Mark’s original version of this page appeared on the front page of Digg and Reddit! (2006-07-13)
If you enjoyed this page, you might also like Code Golf.
Please let me know if you find a shorter Sudoku Solving program.