News for hackers. Stuff that matters.
Re:From hours to 360ms: over-engineering a puzzle solution by gama843 on Sunday February 09, 2025 @08:12AM (#42990461)
Hi, there is a better tool for the job! What you need is a constraint satisfaction solver and 27 All-Different constraints (9 for rows, 9 for cols and 9 for the 3x3 boxes). I just quickly put this together with Claude, because who's got time for writing a custom code for a HN comment. The best I got with Python and a CSP solver is 0.0038 seconds for a single puzzle. I include the code so you can try it yourself and see how it's done.

Daniel

pure python version (Time taken: 0.1341 seconds): https://gist.github.com/gama843/fc183791f77ca1e4d07bdf4afec2...

python with a CSP solver (Time taken: 0.0038 seconds): https://gist.github.com/gama843/b7f88bb2f18e22245cd13cffa82c...

If you want to understand how it works (it specifically mentions Sudoku), have a look at lecture slides (in English) from my uni: http://ktiml.mff.cuni.cz/~bartak/ui/lectures/lecture06eng.pd...

There are no comments on this comment yet.