15pts) Alphahabet Sudoku Solver! Write a solver for the 16x16 letter sudoku puzz
ID: 3839952 • Letter: 1
Question
15pts) Alphahabet Sudoku Solver! Write a solver for the 16x16 letter sudoku puzzle below (you can use numbers from 1 to 16 as well). Test it with a sample starting Sudoku puzzle with some of the letters filled in. Print out a possible solution for the puzzle (make sure you use lower case letters). Submit your Prolog file along with a screen shot of your test. You may use the swipl prompt if you want, or run it as a script, or run it in this interpreter found here http://swish.swi- prolog.org/example/clpfd sudoku.pl 5 Extra Credit points: write it as a script that reads the starting puzzle from a text file and outputs at least one solution to another text.Explanation / Answer
sudoku(partiallyFilledPuzzle, sudukoSolution) :-
length(partiallyFilledPuzzle, L),
puzzleSize is floor(sqrt(L)),
sudukoSolution = partiallyFilledPuzzle,
fd_domain(sudukoSolution, 1, puzzleSize),
slice(partiallyFilledPuzzle, Rows, puzzleSize, 'row'),
slice(partiallyFilledPuzzle, Cols, puzzleSize, 'col'),
slice(partiallyFilledPuzzle, Squares, puzzleSize, 'square'),
valid(Rows),
valid(Cols),
valid(Squares),
pretty_print(Rows).
valid([]).
valid([Head | Tail]) :- fd_all_different(Head), valid(Tail).
sublist_length([], _).
sublist_length([Head | Tail], Length) :- length(Head, Length), sublist_length(Tail, Length).
nth0(I, List, Out) :- I1 is I + 1, nth(I1, List, Out).
insert_into_slice(Item, Values, X, Y) :-
nth0(X, Values, Bucket),
nth0(Y, Bucket, Item).
slice_position('row', puzzleSize, I, X, Y) :-
X is I // puzzleSize,
Y is I mod puzzleSize.
slice_position('col', puzzleSize, I, X, Y) :-
X is I mod puzzleSize,
Y is I // puzzleSize.
slice_position('square', puzzleSize, I, X, Y) :-
Size_Sqrt is floor(sqrt(puzzleSize)),
X is (I mod puzzleSize // Size_Sqrt) + (Size_Sqrt * (I // (puzzleSize * Size_Sqrt))),
Y is (I mod Size_Sqrt) + (Size_Sqrt * ((I mod (puzzleSize * Size_Sqrt)) // puzzleSize)).
slice(partiallyFilledPuzzle, Slice, puzzleSize, Type) :- slice(partiallyFilledPuzzle, Slice, puzzleSize, Type, 0).
slice(_, Slice, puzzleSize, _, I) :- I is puzzleSize * puzzleSize, length(Slice, puzzleSize), sublist_length(Slice, puzzleSize).
slice([Head | Tail], Slice, puzzleSize, Type, I) :-
slice_position(Type, puzzleSize, I, X, Y),
insert_into_slice(Head, Slice, X, Y),
I1 is I + 1,
slice(Tail, Slice, puzzleSize, Type, I1).
pretty_print([Head | Tail]) :-
print(Head),
print(' '),
pretty_print(Tail).
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.