Generating sudokus for fun and no profit
tn1ck.com
tn1ck.com
Do this a couple of times with different videos, and you will start to build a repertoire of techniques yourself. At some point, you will be capable of solving puzzles on your own. (And if you get stuck, the video is there to help you.)
Once upon a time I programmed a solution finder for the game https://en.wikipedia.org/wiki/Ricochet_Robots because a gaming magazine had monthly problems to solve. But I wasn't allowed to run my program until my wife found a solution manually first ;-)
I've been playing a lot with the logic programming language, Prolog. Sudoku is a popular "hello world" for it.
If you haven't used Prolog before, here's an example of a Sudoku solver. It uses Prolog's Constraint Logic Programming over Finite Domains library -- CLP(FD) -- a form of CSP.
https://swish.swi-prolog.org/example/clpfd_sudoku.pl
The relation on line 8 basically encodes the rules of Sudoku verbatim. Logic programming is cool (at least to me) because relations can be run in any direction with any number of variables.
I wonder how writing a Sudoku puzzle generator would differ in a language that had first-class support for CSP.
I wrote a Sudoku solver using a SAT solver compiled to wasm (it is just a simple exercise TBH):
https://www.nhatcher.com/hats/sudoku.html https://www.nhatcher.com/post/on-hats-and-sats/
And the WASM solver is super cool, definitely useful for generating them as you have to do quite a lot of iterations!
Edit: Should be fixed now.
Seems like it's fixed now though, thanks :)
It would make a great dataset for benchmarking different solver approaches.
Though the color of the inserted numbers and the highlight color of the current selected number are nearly the same, would be great if they were much more distinct.
Another thing I noticed is that the wrong entry highlight sometimes highlights multiple correct numbers as incorrect and some static numbers will vanish.
Before:
After:
(Also now red and orange are a bit too similar ^^, maybe introduce the color blue?)
The numbers themselves are all interchangeable, so you have 9! combinations: 362,880.
Columns 1-to-3 are all interchangeable, as are 4-to-6, and 7-to-9. On top of this, these blocks of columns (1-to-3, 4-to-6, 7-to-9) are all interchangeable. Read about wreath products in group theory to know more. Each of the above symmetries are 3!, combined to yield 3! * 3! = 36 combinations. As well as the columns though, the rows have the same property, so those can be combined too: 36 * 36 = 1,296.
Finally, there are the symmetries of a square. Combining all rotations and flips yields a further 8.
In total, sudoku has 3,762,339,840 symmetries. Owing to the starting state of the sudoku puzzle being incomplete, the orbit of the set of points (more group theory) will be smaller than 3 billion, but it provides an efficient method of recreating many more puzzles with the same property. In this case, human complexity.
I think wreath products relate to the second sentence; see this page, which mentions the same result: https://en.wikipedia.org/wiki/Mathematics_of_Sudoku#The_sudo...
So if you have a puzzle that can be solved using only techniques that interested people can come up with fairly readily/intuitively and apply without a lot of ceremony, then that would be, perhaps, very easy. The more advanced techniques (for humans) needed to solve the puzzle, the harder it would be rated.
You can also feed these techniques into the generation so that you can guide the difficulty as it's being generated (the way I did it, I found it would still fall into puzzles that are easier than the target, or get stuck on puzzles that are too hard, but applying adjustments to backtracking and forward progress based on heuristics observed in "stuck" scenarios seemed to do the trick.
> and this makes the whole analysis problematic, as we still don't know if this is actually a good difficulty indicator for how a human perceives the difficulty
> Once upon a time I decided to create a complete sudoku application as my grandma wanted to play some sudokus on her computer and I wasn't satisfied with the free offers available.
I liked the rest too and the website as well, especially the user friendly UX - the "applets" can be paused, the website has all kinds of display options, there are keyboard shortcuts and support for arrow keys.
My dream would be a "made for grandma" embeddable badge - and websites like this becoming a trend in 2024.
And makes me happy you like the applets. I really like creating interactive articles, they can help so much with understanding, https://ciechanow.ski/ articles are the perfect example of this. It's crazy how easier something becomes to grasp if you can play around with it.
Haha, the badge idea is definitely cool! I do fear for my less technical relatives becoming a target of a predatory app that should be free. Would be nice to quickly find good solutions. My trick is normally to search for "github" and find some random programmers project that is free of any monetization strategy e.g. "memory matching github"
I keep it super simple and don't do "the proper way" of things at times (e.g. the blog index is manually done by me). But that keeps it simple & independent to me. Next.js here is just a detail, I can always move to some other React-based static site generator.
It's hosted at Cloudflare.
The design is heavily inspired by https://turbopuffer.com/, I don't deserve any praise for that.
I had actually looked in your GH and hadn't found this - it 404s when I use this link so the repo may be private
This would also work for my old idea of a "use this one, Grandma" generator - basically a way to print out (and annotate with instructions) a layout of a remote control, microwave/washer/etc. interface, or anything else that you might need to walk a relative through setting up or using.
Many years ago I wrote a Sudoku generator in C++ that was based on Knuth’s “dancing links” algorithm. It then analyzed the generated puzzle in terms of what techniques were necessary to solve it, and ranked them accordingly.
Perhaps there is still something useful in there: https://github.com/stlab/adobe_source_libraries/tree/main/te...
I am a broken record on posts that mention sudoku in bringing in Knuth's treatment of it. He has a ton of really fun exercises on the game in the latest volume. Perhaps the most fun are the puzzles that have a single solution, but do not have enough information to place a single piece without ambiguity.
While I like the idea of using ARC3 to grade sudoku, I much prefer the approach developed by Andrew C. Stuart in [0], where they rely on the human techniques* needed to solve the sudoku. Indeed, Sudoku are small enough that a reasonable greedy algorithm is enough to solve them quasi instantly on modern hardware.
* techniques to solve the sudoku that can be applied by an human (as opposed to a computer).
[0]: https://www.sudokuwiki.org/Sudoku_Creation_and_Grading.pdf
https://www.sudokuwiki.org/sudoku.htm
This site is the place where I learned how deep sudoku goes. I was rather naive before I read about the X-wing, Jellyfish, Medusa, and Death Blossom…
[0] https://www.conceptispuzzles.com/index.aspx?uri=puzzle/euid/...
[1] https://www.conceptispuzzles.com/index.aspx?uri=puzzle/sudok...
[2] https://www.conceptispuzzles.com/index.aspx?uri=mobile/10001...
[3] https://www.conceptispuzzles.com/index.aspx?uri=mobile/10001...
https://www.chiark.greenend.org.uk/~sgtatham/puzzles/js/solo...
https://brainium.com/games/sudoku/
It has selectable difficulty and a "hint" mode that teaches you how to solve even the hardest sudokus without any backtracking search at all.
I got at least as far as thinking through something like your list of algorithms here. But I could help but imaging that there must also be even-more efficient or interestingly exotic solutions out there.
Like something amusing as a rainbow-table type approach where you calculate all the possible soduko boards in advance, then (somehow?) convert any given puzzle into just an index lookup of a matching solution. So like (perhaps a lot of) brute force up front, but O(1) in execution?
IMO it's probably too time consuming for a technical interview question.
That's sorta where I am with sudoku, up to a certain difficulty they are fun and challenging, after that it's just brute forcing and crossing off numbers and not fun. I think some of the better deduction techniques can come into play on the harder ones, but I haven't taught myself any of them.