Skip to content

Instantly share code, notes, and snippets.

@brantsch
Created July 25, 2014 07:47
Show Gist options
  • Save brantsch/5131183b59a07c44e301 to your computer and use it in GitHub Desktop.
Save brantsch/5131183b59a07c44e301 to your computer and use it in GitHub Desktop.
Sudoku solver in Prolog
:- use_module(library(clpfd)).
/*
Show a sublist of <Pos> in <Sublist>.
<Offset> Offset into <Pos>
<Step> Step size used in iterating through <Pos>
<Length> Length of the sublist.
*/
getSublist(_,_,_,0,[]). % base case
getSublist(Pos,Offset,Step,Length,Sublist) :-
Idx is Offset + Step*(Length-1),
nth0(Idx,Pos,Elem),
Length_n is Length-1,
getSublist(Pos,Offset,Step,Length_n,Sublist_n),
Sublist = [Elem|Sublist_n].
getBlock(Blockidx,Grid,Block) :-
A is (Blockidx mod 3)*3 + (Blockidx - Blockidx mod 3)*9,
B is A + 9,
C is B + 9,
getSublist(Grid,A,1,3,RowA),
getSublist(Grid,B,1,3,RowB),
getSublist(Grid,C,1,3,RowC),
append(RowC,RowB,RowsBC),
append(RowsBC,RowA,Block).
postConstraints(9,_).
postConstraints(Idx,Grid) :-
Firstinrow is 9*Idx,
getSublist(Grid,Firstinrow,1,9,Row),
all_different(Row),
getSublist(Grid,Idx,9,9,Column),
all_different(Column),
getBlock(Idx,Grid,Block),
all_different(Block),
Idx_n is Idx + 1,
postConstraints(Idx_n,Grid).
postCellConstraints(81,_).
postCellConstraints(Idx,Grid) :-
nth0(Idx,Grid,A),
( integer(A) ->
A #= A
;
true
),
Idx_n is Idx + 1,
postCellConstraints(Idx_n,Grid).
pprint(Grid) :-
\+ (
between(0,8,Idx),
Firstinrow is 9*Idx,
getSublist(Grid,Firstinrow,1,9,Row),
format('~w~n',[Row]),
fail
).
/*
* To have this predicate solve a Sudoku, pass it the puzzle as an 81 element
* list, which is a concatenation of all rows of the puzzle. Fixed, predefined
* numbers shall be at their respective places, fields still to be populated
* shall be left unbound.
*/
sudoku(Grid) :-
length(Grid,81),
Grid ins 1..9,
postCellConstraints(0,Grid),
postConstraints(0,Grid),
label(Grid),
pprint(Grid).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment