

Sudoku Solving - nu2ycombinator
http://norvig.com/sudoku.html

======
muhuk

      Why did I do this? As computer security expert Ben Laurie
      has stated, Sudoku is "a denial of service attack on human
      intellect". My wife was infected by the virus, and I wanted
      to convince her that the problem had been solved and didn't
      need any more of her time.
    

Very true.

~~~
jules
Now lets go back to watching TV!

~~~
adrianN
Nah, that's a solved problem. I let my VCR watch TV for me. Now if I had an
electric monk, I could stop believing in things.

------
mberning
I remember doing something very similar in college. My first cut used a
hideous object model, but my second go at it used a 3 dimensional matrix to
track all of the data and was much faster and space efficient.

The "Why?" at the end of the article pretty much sums up why I can't play most
board games and puzzles. Once you 'solve' sudoku, chess, connect 6, ect. it
really takes the fun out of it, even if your brain doesn't calculate the
solution as quick as your code.

~~~
iki23
Doesn't apply to chess imho. I still keep discovering different views of the
game on occasional plays. The standard algorithm takes too much time, so you
have to find out how to see what's important.

~~~
adrianN
I played chess for a while in a chess club. But to become any good you need to
memorize a lot of positions to be able to efficiently recognize good moves.
Playing creatively to have fun almost always leads to defeat.

------
roryokane
Norvig tries to test his program against the hardest puzzle he can find, but
only tries to find this hard puzzle by generating random ones. The program
Sudoku Susser (<http://www.madoverlord.com/projects/sudoku.t>) actually comes
with “the hardest sudoku in the world”, which I think the author of that
program has proven somehow, so Norvig should try his program on that.

Sudoku Susser can solve sudoku puzzles not only the brute-force way shown in
the article, but also using “human” reasoning, and show you all the steps.

~~~
silentbicycle
I wouldn't call constraint propagation "brute-force". It isn't enumerating
every combination of values and backtracking (as either a non-contstraint
Prolog program or a C program with up to 81 nested for-loops would). Rather,
it's removing every possibility that has already been canceled out (constraint
propagation), then saving undo information, making one guess, and seeing if
that sets off another chain reaction or reaches a solution.

He acknowledges the possibility of using more "reasoning" in the article, but
dismisses it:

" _We could try to code more sophisticated strategies. For example, the naked
twins strategy looks for two squares in the same unit that both have the same
two possible digits. [...] Coding up strategies like this is a possible route,
but would require hundreds of lines of code (there are dozens of these
strategies), and we'd never be sure if we could solve every puzzle._ "

If you want to read more about constraint programming, there is an excellent
overview chapter in CTM (<http://www.info.ucl.ac.be/~pvr/book.html>). "The Art
of the Propagator" (<http://dspace.mit.edu/handle/1721.1/44215>) is also good,
though it focuses more on arithmetic value propagation than set propagation
problems like sudoku. For more advanced material, look at the clp(FD) papers
by Danial Diaz ([http://cri-dist.univ-paris1.fr/diaz/publications/cv-
short.ht...](http://cri-dist.univ-paris1.fr/diaz/publications/cv-short.html));
familiarity with Prolog terminology will be helpful.

------
jonah
He could have used Mechanical Turk to solve them. ;)

Growing up, we had a favorite game. That is until my sister solved it in that
going first you could _always_ win. It wasn't as fun after that.

~~~
mryall
<http://xkcd.com/832/>

~~~
jonah
I like that one.

Ours was a different game: Mühle. Also Mill or Nine Men's Morris.

I just discovered that Ralph Gasser solved it in 1996 using retrograde
analysis and an 18-ply alpha- beta search. [1] Becoming "the first non-trivial
game to be solved that does not seem to benefit from knowledge-based methods."

[1] <http://library.msri.org/books/Book29/files/gasser.pdf>

------
ozataman
Would be interesting to see how this fares performance-wise in comparison:

[http://corp.galois.com/blog/2009/3/18/solving-sudoku-
using-c...](http://corp.galois.com/blog/2009/3/18/solving-sudoku-using-
cryptol.html)

It's a sudoku solver based on Cryptol, which is "... a language tailored for
cryptographic algorithms." built on top of Haskell. The amazing this is that
all you need to define is a function that checks whether a given board is
solved. Cryptol does the searching for you!

------
jws
I find that calculating the sequence of stereo pairs for an MP3 file is much
simpler and more accurate if I dispense with the human listener.

For instance, I just rendered "In the Year 2525" 593 times faster than a human
can listen to it on a single thread of a core i3.

------
wallflower
Reminds me of:

[http://blog.jgc.org/2010/01/more-fun-with-toys-ikea-
lillabo-...](http://blog.jgc.org/2010/01/more-fun-with-toys-ikea-lillabo-
train.html)

------
YuriNiyazov
Can someone explain the Ben Laurie quote? I know what Sudoku is, and I know
what a DOS attack is. I don't see the connection between the two.

~~~
JoshCole
Sudoku has a human using their mind to solve things which don't need to be
solved. A denial of service attack leaves a computer trying to handle things
which don't need to be handled. So they have something trying to deal with
things that don't need to be dealt with.

~~~
YuriNiyazov
Thank you.

------
atuladhar
I wrote one in JavaScript a long time ago, again using a simple backtracking
algorithm.

<http://www.amrittuladhar.com/projects/sudokusolver/>

EDIT: Just realized the "load puzzle" feature doesn't seem to work on Chrome,
but it does in other browsers.

------
xtacy
If you really want speed, then I would recommend using a good implementation
of Dancing Links for solving constraint satisfaction problems. Don Knuth
proposes a doubly linked list structure to speed up recursive state space
exploration: www-cs-faculty.stanford.edu/~uno/papers/dancing-color.ps.gz.

~~~
bobfunk
Mentioned this below as well, but got a pretty straight forward ruby
implementation of Knuths Dancing Links up at:

<https://github.com/biilmann/Ruby-DLX-Sudoku-Solver>

It's not hyper-fast (for speed I actually implemented it as a c-extention to
ruby, but it's a long time ago and I don't think I have the code around by
now) but being ruby it's fairly easy to read.

Can really recommend reading the paper on Dancing Links and playing around
with the algorithm, its such a great feeling once you start visualizing how
the linked list trick works :)

~~~
jmelloy
Dancing Links is definitely a fun algorithm to implement. I wrote one in
Python a while back and included an option to generate graphs as it went
through the recursion tree. I put up a quick page at
<http://cavernum.net/dlsudoku/> demonstrating them.

------
jws
Sadly it is a backtracking algorithm which is not proper Sudoku solving
technique. (Opinions vary on the topic.)

~~~
crux_
I'm curious... what would be the 'proper' technique?

~~~
nazgulnarsil
solving without trial and error at all.

~~~
crux_
That strikes me as _extremely unlikely_.

I think the complexity class of Sudoko is NP-Complete (a quick google confirms
it's not exactly NP but very close:
<http://11011110.livejournal.com/23221.html?thread=19381> \-- complexity is
the same as solving SAT problems which have only one unique solution)

~~~
pieceofpeace
Glenn Flower has written an (open source) program that solves using constraint
propagation only. It does solve some of the hardest Sudoku's.

He discusses the strategies used here:
<http://www2.research.att.com/~gsf/sudoku/>

~~~
crux_
From the first paragraph of your link:

> The solver uses depth first and/or breadth first with constraint propagation
> to prune the search

That would be backtracking.

Further, the purpose of many the constraint techniques seem to be aimed at
creating puzzles that are amenable to humans, not solving them. The article
itself states that for solving, backtracking with fewer constraints performs
better.

~~~
pieceofpeace
You are right that "the purpose of many the constraint techniques seem to be
aimed at creating puzzles that are amenable to humans, not solving them"

I shared the link because I found his sudoku generator and the constraint
methods interesting.

> That would be backtracking.

Let me quote the first para in full:

    
    
      The solver uses depth first and/or breadth first tree search
      with constraint propagation to prune the search for the next
      best move (forms of forward checking.) There are space/time
      tradeoffs between depth/breadth first search and the constraints
      used; sudoku(1) has options to control the combinations.
      The common characteristic for all constraints, here and elsewhere,
      is that they avoid trial and error. Its fine for a computer
      to guess and backtrack but a definite breach of puzzle manners
      to require a human to do so.
    

I could be wrong but, yes, it does use backtracking to find the _constraint_
that can give a number for an empty cell but it never has to change a number
it has put in a cell. That differs from the trial and error approach that
moves forward by guessing values and checking if it leads to a valid solution.

~~~
crux_
The constraint-finding is for _generating_ puzzles that are easy, hence the
comment about puzzle manners.

------
isak2
I wrote one in C# a while ago:

[http://isaksky.wordpress.com/2010/10/30/objected-oriented-
so...](http://isaksky.wordpress.com/2010/10/30/objected-oriented-solution-to-
every-sudoku-puzzle-in-csharp/)

~~~
gumbo
I did the same, need to dig into my back up hard drive to find it. Maybe i'm
going to expose a REST api for generating sudokus. Do you think this might be
interesting?

"Did you know that in order to generate a sudoku you need first to solve it?"

------
akivabamberger
The answer is just 3 words: depth first search. I don't get why such a tedious
article was written for such a simple, common, and obvious solution.

------
alienDeveloper
<http://news.ycombinator.com/item?id=2374763>

another php version

