{"id":4,"date":"2007-04-01T22:36:41","date_gmt":"2007-04-01T22:36:41","guid":{"rendered":"http:\/\/www.coolfactor.org\/blog\/2007\/04\/01\/sudoku-solver\/"},"modified":"2007-04-01T22:36:41","modified_gmt":"2007-04-01T22:36:41","slug":"sudoku-solver","status":"publish","type":"post","link":"http:\/\/www.coolfactor.org\/blog\/2007\/04\/01\/sudoku-solver\/","title":{"rendered":"Sudoku solver"},"content":{"rendered":"<p>I&#8217;ve been working on my sudoku solver.<\/p>\n<p>It&#8217;s not the most efficient sudoku solver, by a long way. But the goal is not really just to solve sudoku. It&#8217;s to solve sudoku using pure logic, and to be able to show a logical path to the solution. If I wanted to just solve sudoku, I&#8217;d implement DLX.<\/p>\n<p>The basic principles are quite simple. The problem is expressed in terms of logical statements constructed as disjunctions of predicates, where a predicate takes the form of &#8220;Box (X,Y) contains Z&#8221;, or &#8220;Box (X, Y) does not contain Z&#8221;.<\/p>\n<p>There is essentially one logical deduction rule used. Two statements are resolved to produce one or more new statements by finding a eliminating complementary terms and joining up the result with an OR. For example, &#8220;(8,7) isn&#8217;t 5 or (8,8) isn&#8217;t 5&#8221; can be resolved with &#8220;(8,8) is 5&#8221; to produce &#8220;(8,7) isn&#8217;t 5&#8221;.<\/p>\n<p>There is a constraint on the knowledge base which is that no statement can be equal to, or be a subset of, any other statement.<\/p>\n<p>For (some kind of) efficiency, statements with a smaller number of terms are looked at first. Looking at a term involves resolving it with all other statements with which it can be resolved.<\/p>\n<p>This converges pretty quickly for simple sudoku grids, but can take much longer for hard ones. So far I&#8217;ve tried it on some of the hardest &#8220;unsolvable&#8221; puzzles I&#8217;ve been able to find on the Internet, and while it does take a very long time for those (over an hour for some puzzles) it does eventually converge and give a correct result.<\/p>\n<p>Perhaps one of the more interesting aspects of a solution using this method is that the solver can construct a DAG of the entire proof. The downside of this is that I now have to figure out a way of actually presenting that DAG in a simple enough way that it can be understood by a human. Some sort of pattern matching on the graph to remove &#8220;obvious&#8221; steps could be one way to go.<\/p>\n<p>For kicks, I tried constructing a dot file from the DAG generated by solving a relatively simple sudoku puzzle, and passing it through dot. The result is an unpleasantly large 110 megabyte postscript file, spanning 285 pages.<\/p>\n<p>I&#8217;m looking forward to finding some way to express the proofs more intuitively.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working on my sudoku solver. It&#8217;s not the most efficient sudoku solver, by a long way. But the goal is not really just to solve sudoku. It&#8217;s to solve sudoku using pure logic, and to be able to &hellip; <a href=\"http:\/\/www.coolfactor.org\/blog\/2007\/04\/01\/sudoku-solver\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.coolfactor.org\/blog\/wp-json\/wp\/v2\/posts\/4"}],"collection":[{"href":"http:\/\/www.coolfactor.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.coolfactor.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.coolfactor.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.coolfactor.org\/blog\/wp-json\/wp\/v2\/comments?post=4"}],"version-history":[{"count":0,"href":"http:\/\/www.coolfactor.org\/blog\/wp-json\/wp\/v2\/posts\/4\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.coolfactor.org\/blog\/wp-json\/wp\/v2\/media?parent=4"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.coolfactor.org\/blog\/wp-json\/wp\/v2\/categories?post=4"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.coolfactor.org\/blog\/wp-json\/wp\/v2\/tags?post=4"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}