Solving Every Sudoku Puzzle (2006)
norvig.com
norvig.com
For example (2x2 sudoku = 4 sheets):
1324
3142
4213
2431
is: 1... .2.. .3.. ...4
.1.. ...2 3... ..4.
..1. + .2.. + ...3 + 4...
...1 2... ..3. .4..
or even: x... .x.. .x.. ...x
.x.. ...x x... ..x.
..x. + .x.. + ...x + x...
...x x... ..x. .x..
x... .x.. ..x. ...x <- number row
where the last "number row" does not belong to the grid but serves to indicate the number of the sheet.This explains how sudoku relates to the Exact Cover problem. Now, there is a good algorithm by Knuth to solve Exact Cover, which is known as Algorithm X [2].
Algorithm X is just an algorithm but Knuth has also invented a very efficient implementation of it, called Dancing Links or DLX for short [3]. DLX is described in a very good and very readable paper by Knuth himself [4]. This is one of the first serious papers I ever read and it was a real joy, mind-opener.
[1] https://en.wikipedia.org/wiki/Exact_cover
[2] https://en.wikipedia.org/wiki/Knuth%27s_Algorithm_X
[3] https://en.wikipedia.org/wiki/Dancing_Links
[4] http://www-cs-faculty.stanford.edu/~uno/papers/dancing-color...
Sudoku solver in three lines of Perl (golf): https://web.archive.org/web/20070106133158/http://www.eccles...
use integer;@A=split//,<>;sub R{for$i(0..80){next if$A[$i];my%t=map{$_/9
==$i/9||$_%9==$i%9||$_/27==$i/27&&$_%9/3==$i%9/3?$A[$_]:0=>1}0..80;R($A[
$i]=$_)for grep{!$t{$_}}1..9;return$A[$i]=0}die@A}RHere are some other sudoku solvers that give more insight into the coder than the problem:
Sudoku in APL: https://www.youtube.com/watch?v=DmT80OseAGs
Test driven sudoku: http://ronjeffries.com/xprog/articles/sudokumusings/
In any case, I'm replying to your comment because it applies to me. Norvig's code is compact and elegant, whereas mine is spaghetti code at best. I basically have lots of control statements, as there are many many processing steps that I painstakingly discovered as I went along (thousands of lines). Heck, I can barely remember them myself.
Here's my site: http://sudokuisland.com
To be fair, the reason Norvig was so successful was he knew exactly the sort of problem soduku was and how to solve it, whereas Ron lacked this background knowledge.
In TDD your test will always predate your understanding. So it forces you to solve (part of) the problem in your head first, rather than combine the act of problem-solving and writing. And you do this over and over again. And when you change your mind about something, you have twice the code to refactor.
It's a difference between recording your thinking in code and actually thinking in code.
This reminds me of an old article that exemplifies this: http://insideofthebox.tumblr.com/post/52002125683/prime-fact...
Prime factor kata is often used as an introductory example of TDD, yet it is clearly impractical and produces garbage code. Not to mention that I've actually seen people do it, stumble, and hectically reach for their notes to see what's the next step. And it's a pretty darn simple problem.
Their approach was quite close to solving every sudoku puzzle, although they traversed the space by the solutions.
In fact in the ‘Translations’ section at the end there’s a link to this Bugzilla ticket: https://bugzilla.mozilla.org/show_bug.cgi?id=380237
It seems like he started with lists and switched when he got to the search method ("This is why I chose to implement the set of possible values for a square as a string: I can copy values with values.copy() which is simple and efficient.") - It's probably not an amazing insight, but I'm sure my brain would have been yelling at me "don't use strings! don't use strings!".
I was learning algorithms then and a sudoku solver was both fun and a good learning exercise.
https://github.com/ssadler/ssadler-various/blob/master/sudok...
(This code is completely indulgent but very fast!)
Not all of the code is efficient, but it's interesting nevertheless.
This was tested with my own algorithm and one supposedly fast found online. I will revise my code, but I doubt I will improve the speed.
Instead, as you suggest, staving off cognitive decline might be better accomplished by learning some Python, learning a new language well enough to do some traveling or reading with it, learning to play a new musical instrument, learning some simple juggling, or whatever is both safe and more cognitively taxing than polishing an existing skill by repetition.
What's better for the brain, solving Sudokus, or writing a Sudoku solver?
Both can be good. Chances are that your first Sudoku solver uses some bruteforce approach, while your manual solution certainly does not. Then the intuitions that you get from many manual games can drive the design of your solver. Of course you can implement some established algorithm but I don't see that challenging.
I have mathematician friend who won't ever play Nine Men's Morris because it's a solved game, so it's not longer interesting for him. It's a shame, you can learn a lot even playing Nim variants if you don't already know the winning strategy, it's a good learning material for kids too.