Skip to content

Instantly share code, notes, and snippets.

@passingloop
Created October 12, 2011 14:15
Show Gist options
  • Save passingloop/1281330 to your computer and use it in GitHub Desktop.
Save passingloop/1281330 to your computer and use it in GitHub Desktop.
7l7w-prolog-day-3
% -*- mode: prolog; -*-
valid([]).
valid([Head|Tail]) :-
fd_all_different(Head),
valid(Tail).
sudoku(Puzzle, Solution) :-
length(Puzzle, 16),
sudoku4(Puzzle, Solution).
sudoku(Puzzle, Solution) :-
length(Puzzle, 36),
sudoku6(Puzzle, Solution).
sudoku(Puzzle, Solution) :-
length(Puzzle, 81),
sudoku9(Puzzle, Solution).
sudoku4(Puzzle, Solution) :-
Solution = Puzzle,
Puzzle = [S11, S12, S13, S14,
S21, S22, S23, S24,
S31, S32, S33, S34,
S41, S42, S43, S44],
fd_domain(Solution, 1, 4),
Row1 = [S11, S12, S13, S14],
Row2 = [S21, S22, S23, S24],
Row3 = [S31, S32, S33, S34],
Row4 = [S41, S42, S43, S44],
Col1 = [S11, S21, S31, S41],
Col2 = [S12, S22, S32, S42],
Col3 = [S13, S23, S33, S43],
Col4 = [S14, S24, S34, S44],
Square1 = [S11, S12, S21, S22],
Square2 = [S13, S14, S23, S24],
Square3 = [S31, S32, S41, S42],
Square4 = [S33, S34, S43, S44],
valid([Row1, Row2, Row3, Row4,
Col1, Col2, Col3, Col4,
Square1, Square2, Square3, Square4]),
print(Row1), nl,
print(Row2), nl,
print(Row3), nl,
print(Row4), nl.
sudoku6(Puzzle, Solution) :-
Solution = Puzzle,
Puzzle = [S11, S12, S13, S14, S15, S16,
S21, S22, S23, S24, S25, S26,
S31, S32, S33, S34, S35, S36,
S41, S42, S43, S44, S45, S46,
S51, S52, S53, S54, S55, S56,
S61, S62, S63, S64, S65, S66],
fd_domain(Solution, 1, 6),
Row1 = [S11, S12, S13, S14, S15, S16],
Row2 = [S21, S22, S23, S24, S25, S26],
Row3 = [S31, S32, S33, S34, S35, S36],
Row4 = [S41, S42, S43, S44, S45, S46],
Row5 = [S51, S52, S53, S54, S55, S56],
Row6 = [S61, S62, S63, S64, S65, S66],
Col1 = [S11, S21, S31, S41, S51, S61],
Col2 = [S12, S22, S32, S42, S52, S62],
Col3 = [S13, S23, S33, S43, S53, S63],
Col4 = [S14, S24, S34, S44, S54, S64],
Col5 = [S15, S25, S35, S45, S55, S65],
Col6 = [S16, S26, S36, S46, S56, S66],
Rect1 = [S11, S12, S13, S21, S22, S23],
Rect2 = [S14, S15, S16, S24, S25, S26],
Rect3 = [S31, S32, S33, S41, S42, S43],
Rect4 = [S34, S35, S36, S44, S45, S46],
Rect5 = [S51, S52, S53, S61, S62, S63],
Rect6 = [S54, S55, S56, S64, S65, S66],
valid([Row1, Row2, Row3, Row4, Row5, Row6,
Col1, Col2, Col3, Col4, Col5, Col6,
Rect1, Rect2, Rect3, Rect4, Rect5, Rect6]),
print(Row1), nl,
print(Row2), nl,
print(Row3), nl,
print(Row4), nl,
print(Row5), nl,
print(Row6), nl.
sudoku9(Puzzle, Solution) :-
Puzzle = Solution.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment