Courses/CS 460/Fall 2005/Basic Sudoku solver/earlierSudokusolver
From CSWiki
< Courses | CS 460 | Fall 2005 | Basic Sudoku solver
Here is the original Sudoku Solver.
local
fun {Script Spec}
proc {$ Root}
Root={Map {List.make 9}
fun {$ Row}
Row={List.make 9}
Row ::: 1#9
%% All numbers in each row are distinct
{FD.distinct Row}
Row
end
}
%% All numbers in each column are distinct
for X in 1;X=<9;X+1 do
{FD.distinct
{FoldR Root fun {$ Row Prev} {Nth Row X}|Prev end nil}
}
end
%% All numbers in each 3x3 square are distinct
for X in 0;X=<2;X+1 do
for Y in 0;Y=<2;Y+1 do
{FD.distinct
{FoldR
{List.filterInd Root fun {$ I Row} I>=1+Y*3 andthen I=<3+Y*3 end}
fun {$ Row Prev}
{Append
{List.filterInd Row fun {$ I X} I>=1+Y*3 andthen I=<3+Y*3 end}
Prev
}
end
nil
}
}
end
end
%% Process specs
{List.forAllInd Spec
proc {$ Y R}
Row={Nth Root Y}
in
{List.forAllInd R
proc {$ X I}
if {Not {IsFree I}} then
{Nth Row X} :: I
end
end
}
end
}
%% Search...
{FD.distribute ff {Flatten Root}}
end
end
in
{ForAll {Nth
{SearchOne {Script [
[_ 9 _ 3 _ _ _ 1 _]
[2 _ 3 _ _ 1 _ _ _]
[_ _ _ _ 7 5 _ 8 _]
[_ _ 2 _ _ _ _ 5 6]
[_ _ _ 7 _ 8 _ _ _]
[7 1 _ _ _ _ 2 _ _]
[_ 2 _ 4 9 _ _ _ _]
[_ _ _ 8 _ _ 5 _ 1]
[_ 7 _ _ _ 2 _ 9 _]
]
}
}
1
}
Show
}
end

