Skip to content

Instantly share code, notes, and snippets.

@muxuezi
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muxuezi/7adca6cfcb4f6b9e142a to your computer and use it in GitHub Desktop.
Save muxuezi/7adca6cfcb4f6b9e142a to your computer and use it in GitHub Desktop.
7Weeks7languages--prolog
%-- Thanks Ben Nadel for his blog http://www.bennadel.com/blog/2074-seven-languages-in-seven-weeks-prolog-day-3.htm
%-- I am the 9x9 solution.
%-- Empty boards are valid.
valid( [] ).
%-- The sets are valid if each set contains unique values.
valid([Head|Tail]) :-
fd_all_different(Head),
valid(Tail).
sudoku( Solution, Puzzle) :-
%-- Assert that this is only true if the Puzzle and the
%-- Solution are the same.
Solution = Puzzle,
%-- Break the puzzle out into variables, one for each square.
Puzzle = [A1, A2, A3, A4, A5, A6, A7, A8, A9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
C1, C2, C3, C4, C5, C6, C7, C8, C9,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
E1, E2, E3, E4, E5, E6, E7, E8, E9,
F1, F2, F3, F4, F5, F6, F7, F8, F9,
G1, G2, G3, G4, G5, G6, G7, G8, G9,
H1, H2, H3, H4, H5, H6, H7, H8, H9,
I1, I2, I3, I4, I5, I6, I7, I8, I9],
%-- Assert that each element within the list (which represents
%-- the board, is in the range 1-9).
fd_domain( Puzzle, 1, 9 ),
%-- Define each row as a collection of cells.
Row1 = [A1, A2, A3, A4, A5, A6, A7, A8, A9],
Row2 = [B1, B2, B3, B4, B5, B6, B7, B8, B9],
Row3 = [C1, C2, C3, C4, C5, C6, C7, C8, C9],
Row4 = [D1, D2, D3, D4, D5, D6, D7, D8, D9],
Row5 = [E1, E2, E3, E4, E5, E6, E7, E8, E9],
Row6 = [F1, F2, F3, F4, F5, F6, F7, F8, F9],
Row7 = [G1, G2, G3, G4, G5, G6, G7, G8, G9],
Row8 = [H1, H2, H3, H4, H5, H6, H7, H8, H9],
Row9 = [I1, I2, I3, I4, I5, I6, I7, I8, I9],
%-- Define each column as a collection of cells.
Col1 = [A1, B1, C1, D1, E1, F1, G1, H1, I1],
Col2 = [A2, B2, C2, D2, E2, F2, G2, H2, I2],
Col3 = [A3, B3, C3, D3, E3, F3, G3, H3, I3],
Col4 = [A4, B4, C4, D4, E4, F4, G4, H4, I4],
Col5 = [A5, B5, C5, D5, E5, F5, G5, H5, I5],
Col6 = [A6, B6, C6, D6, E6, F6, G6, H6, I6],
Col7 = [A7, B7, C7, D7, E7, F7, G7, H7, I7],
Col8 = [A8, B8, C8, D8, E8, F8, G8, H8, I8],
Col9 = [A9, B9, C9, D9, E9, F9, G9, H9, I9],
%-- Define each square as a collection of cells.
Square1 = [A1, A2, A3, B1, B2, B3, C1, C2, C3],
Square2 = [A4, A5, A6, B4, B5, B6, C4, C5, C6],
Square3 = [A7, A8, A9, B7, B8, B9, C7, C8, C9],
Square4 = [D1, D2, D3, E1, E2, E3, F1, F2, F3],
Square5 = [D4, D5, D6, E4, E5, E6, F4, F5, F6],
Square6 = [D7, D8, D9, E7, E8, E9, F7, F8, F9],
Square7 = [G1, G2, G3, H1, H2, H3, I1, I2, I3],
Square8 = [G4, G5, G6, H4, H5, H6, I4, I5, I6],
Square9 = [G7, G8, G9, H7, H8, H9, I7, I8, I9],
%-- Asser that all the rows, columns, and squares are valid; that
%-- is, they contain unique values, 1-9.
valid([Row1, Row2, Row3, Row4, Row5, Row6, Row7, Row8, Row9,
Col1, Col2, Col3, Col4, Col5, Col6, Col7, Col8, Col9,
Square1, Square2, Square3, Square4, Square5, Square6, Square7, Square8, Square9]),
%-- get the unique solutions
fd_labeling(Solution),
%-- Now that all of the variables have been unified (otherwise,
%-- we wouldn't have gotten this far), we can simply rely on the
%-- fact that the Row variables contain valid answer. Just print
%-- each row in its own line.
write( '\n' ), write( Row1 ),
write( '\n' ), write( Row2 ),
write( '\n' ), write( Row3 ),
write( '\n' ), write( Row4 ),
write( '\n' ), write( Row5 ),
write( '\n' ), write( Row6 ),
write( '\n' ), write( Row7 ),
write( '\n' ), write( Row8 ),
write( '\n' ), write( Row9 )
.
try(Puzzle) :-
sudoku(Solution,[
_,_,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,_,_]).
%-- solution
%-- [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]
try2(Puzzle) :-
sudoku(Solution,[
1,_,_,5,_,_,4,_,_,
6,_,_,2,3,_,_,_,_,
_,_,_,_,_,9,5,8,_,
_,_,_,3,_,_,_,6,4,
_,7,_,_,_,_,_,1,_,
8,6,_,_,_,2,_,_,_,
_,5,9,7,_,_,_,_,_,
_,_,_,_,4,3,_,_,2,
_,_,6,_,_,5,_,_,9]).
%-- solution
%-- [1,9,8,5,7,6,4,2,3]
%-- [6,4,5,2,3,8,7,9,1]
%-- [3,2,7,4,1,9,5,8,6]
%-- [5,1,2,3,9,7,8,6,4]
%-- [9,7,3,6,8,4,2,1,5]
%-- [8,6,4,1,5,2,9,3,7]
%-- [2,5,9,7,6,1,3,4,8]
%-- [7,8,1,9,4,3,6,5,2]
%-- [4,3,6,8,2,5,1,7,9]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment