leah blogs

November 2005

17nov2005 · A Sudoku Solver in Prolog

More and more people were requesting it, so I decided to publish my Sudoku solver, written for GNU Prolog and its finite domain solver. You probably can’t easily port it to some other Prolog, but it’s very efficient, despite being completely dumb code.

However, my Prolog knowledge approaches epsilon and I think the code is rather ugly, but it works. Do with it whatever you want.

Example of use:

#1104<1|>lilith:~$ gprolog 
GNU Prolog 1.2.16
By Daniel Diaz
Copyright (C) 1999-2002 Daniel Diaz
| ?- [sudoku].
| ?- metabox(normal, L,
             _,_,1,_,_,_,8,_,_,
             _,7,_,3,1,_,_,9,_,
             3,_,_,_,4,5,_,_,7,
             _,9,_,7,_,_,5,_,_,
             _,4,2,_,5,_,1,3,_,
             _,_,3,_,_,9,_,4,_,
             2,_,_,5,7,_,_,_,4,
             _,3,_,_,9,1,_,6,_,
             _,_,4,_,_,_,3,_,_).

L = [4,2,1,9,6,7,8,5,3,6,7,5,3,1,8,4,9,2,3,8,9,2,4,5,6,1,7,1,9,8,7,
3,4,5,2,6,7,4,2,8,5,6,1,3,9,5,6,3,1,2,9,7,4,8,2,1,6,5,7,3,9,8,4,8,
3,7,4,9,1,2,6,5,9,5,4,6,8,2,3,7,1] ? ;

no

Very basic and unpolished, but the valid solution. Don’t remind me of the code when you meet me. ;-)

NP: Elliott Smith—Passing Feeling

Copyright © 2004–2022