David Nolen's sudoku solver not compatible with Peter Norvig's generator?

228 views
Skip to first unread message

Jim - FooBar();

unread,
Jan 30, 2014, 9:01:22 AM1/30/14
to clo...@googlegroups.com
Hi all,

I think we are all familiar with the wonderful core.logic implementation of a sudoku solver by David Nolen. Now, I am trying to combine his solver with the `random-puzzle` generator  shown here ( http://jkkramer.com/sudoku.html). I have made the necessary changes (to deal with seqs of numbers rather than strings and to return 0 instead `.` for empty squares) but doing `(solve (random-puzzle))` eats up memory without returning anything, whereas trying out some of the ready made puzzles shown as examples on the above link works!

I suspected a different board representation between the 2 projects but then I noticed that the ready-made examples work in both implementations so it can't be that...both implementations seem to accept a 1D seq of either strings or numbers.

any clues? I'd like to be able to generate random-puzzles that can be passed to the core.logic solver. Currently `(random-puzzle)` returns something like this which at first glance seems compatible with David's solver:

user=> (random-puzzle 17)
(0 0 0 0 0 2 0 0 6 8 0 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 7 0 0 0 0 0 0 3 0 8 2 0 0 0 0 0 0 0 1 0 0 0 3 0 0 0 0 2 7 0 0 0 5 0 8 0 3 0 2 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0)
user=> (count (filter (complement zero?) *1))
17

has anyone looked at both projects?

thanks in advance :)

Jim


martin_clausen

unread,
Jan 30, 2014, 7:40:42 PM1/30/14
to clo...@googlegroups.com
Hi Jim

Using this version of Davids solver and and the random puzzle generator you refer to the below works for me:

(sudokufd (vec (map #(if (= % \.) 0 (Integer. (str %))) (random-puzzle))))

I did adapt David's solver to work with core.logic 0.8.5, but only changes needed were due to fd being in its own namespace.

Martin

Norman Richards

unread,
Jan 30, 2014, 7:47:12 PM1/30/14
to clo...@googlegroups.com
On Thu, Jan 30, 2014 at 8:01 AM, Jim - FooBar(); <jimpi...@gmail.com> wrote:
Now, I am trying to combine his solver with the `random-puzzle` generator  shown here ( http://jkkramer.com/sudoku.html). 
[...]

any clues? I'd like to be able to generate random-puzzles that can be passed to the core.logic solver. Currently `(random-puzzle)` returns something like this which at first glance seems compatible with David's solver:

user=> (random-puzzle 17)
(0 0 0 0 0 2 0 0 6 8 0 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 7 0 0 0 0 0 0 3 0 8 2 0 0 0 0 0 0 0 1 0 0 0 3 0 0 0 0 2 7 0 0 0 5 0 8 0 3 0 2 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0)

Well, first off, that isn't a sudoku.  It has a large number of solutions, and a sudoku only has one solution.  I'm using my slightly modified version of David's sudoku solver (https://gist.github.com/orb/8722569) but I haven't changed anything core to the problem - it's just the version I have laying around:

logic.sudoku> (def maybe-a-sudoku '(0 0 0 0 0 2 0 0 6 8 0 0 0 0 0 0 0 0 0 0 0 5 0 0 0 0 7 0 0 0 0 0 0 3 0 8 2 0 0 0 0 0 0 0 1 0 0 0 3 0 0 0 0 2 7 0 0 0 5 0 8 0 3 0 2 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0))
#'logic.sudoku/maybe-a-sudoku

logic.sudoku> (pprint (partition 9 (first (solve maybe-a-sudoku))))
((1 3 5 4 7 2 9 8 6)
 (8 4 7 1 6 9 2 3 5)
 (6 9 2 5 3 8 1 4 7)
 (4 1 6 2 9 5 3 7 8)
 (2 5 3 6 8 7 4 9 1)
 (9 7 8 3 4 1 5 6 2)
 (7 6 1 9 5 4 8 2 3)
 (3 2 9 8 1 6 7 5 4)
 (5 8 4 7 2 3 6 1 9))
nil

Well, that's one, but clearly there's more.

logic.sudoku> (pprint (partition 9 (first (drop 100 (solve maybe-a-sudoku)))))
((1 3 4 9 7 2 5 8 6)
 (8 5 7 6 3 1 2 4 9)
 (6 9 2 5 4 8 1 3 7)
 (4 1 5 2 6 7 3 9 8)
 (2 6 3 4 8 9 7 5 1)
 (9 7 8 3 1 5 4 6 2)
 (7 4 9 1 5 6 8 2 3)
 (5 2 1 8 9 3 6 7 4)
 (3 8 6 7 2 4 9 1 5))
nil

Lots more...

logic.sudoku> (pprint (partition 9 (first (drop 10000 (solve maybe-a-sudoku)))))
((3 7 9 1 4 2 5 8 6)
 (8 1 5 7 6 3 2 4 9)
 (4 6 2 5 8 9 1 3 7)
 (1 4 6 2 7 5 3 9 8)
 (2 3 7 4 9 8 6 5 1)
 (9 5 8 3 1 6 4 7 2)
 (7 9 4 6 5 1 8 2 3)
 (5 2 1 8 3 7 9 6 4)
 (6 8 3 9 2 4 7 1 5))


I don't know how many solutions there are, but it seems like there are a lot...  I think you are just generating all of them and it's taking a long time.


 

Jim - FooBar();

unread,
Jan 31, 2014, 4:52:40 AM1/31/14
to clo...@googlegroups.com
On 31/01/14 00:47, Norman Richards wrote:
>
> I don't know how many solutions there are, but it seems like there are
> a lot... I think you are just generating all of them and it's taking
> a long time.

spot on!!! I was just being stupid (again)...tried with `first` and
worked immediately :)

Thanks!

Jim
Reply all
Reply to author
Forward
0 new messages