// Abstract ------------------------------------------------------------------------------------------------------------------------------
This sample solves sudoku 2x2 puzzles (the puzzle is given row by row).
// Examples ------------------------------------------------------------------------------------------------------------------------------
?-
#sudoku2x2([[1,_,2,_],[_,_,_,_],[_,_,_,_],[_,4,_,1]],:s)
-> ( [[1, 3, 2, 4], [4, 2, 1, 3], [3, 1, 4, 2], [2, 4, 3, 1]] ) := 1.00 (0.428) 1
?-
#sudoku2x2([[_,_,1,_],[_,_,3,2],[1,4,_,_],[_,2,_,_]],:s)
-> ( [[2, 3, 1, 4], [4, 1, 3, 2], [1, 4, 2, 3], [3, 2, 4, 1]] ) := 1.00 (0.187) 1
// Code ----------------------------------------------------------------------------------------------------------------------------------
box2x2 { // generate all possible combinations of a 2x2 "box" (a list of 4 items)
(:l) :- lst.length(:l,4), lst.span(<1|4>,:l);
}
grid2x2 { // generate all possible combinations of a 2x2 "grid" and very that rows and cols don't have
// duplicated elements
([:a1,:b1,:a2,:b2],
[:c1,:d1,:c2,:d2],
[:a3,:b3,:a4,:b4],
[:c3,:d3,:c4,:d4]) :- #box2x2([:a1,:b1,:c1,:d1]),
#box2x2([:a2,:b2,:c2,:d2]),
#box2x2([:a3,:b3,:c3,:d3]),
#box2x2([:a4,:b4,:c4,:d4]),
lst.diff([:a1,:b1,:a2,:b2]),
lst.diff([:c1,:d1,:c2,:d2]),
lst.diff([:a3,:b3,:a4,:b4]),
lst.diff([:c3,:d3,:c4,:d4]),
lst.diff([:a1,:c1,:a3,:c3]),
lst.diff([:b1,:d1,:b3,:d3]),
lst.diff([:a2,:c2,:a4,:c4]),
lst.diff([:b2,:d2,:b4,:d4]);
}
sudoku2x2 { // main
([:r1,:r2,:r3,:r4],:s) :- #grid2x2(:r1,:r2,:r3,:r4), set([:r1,:r2,:r3,:r4],:s);
}
// ---------------------------------------------------------------------------------------------------------------------------------------
[Home] [Email] [Twitter] [LinkedIn]