Skip to content

Instantly share code, notes, and snippets.

@hsinewu
Last active December 19, 2017 08:59
Show Gist options
  • Save hsinewu/0265ba382738f8a896adbec6f5abd8c3 to your computer and use it in GitHub Desktop.
Save hsinewu/0265ba382738f8a896adbec6f5abd8c3 to your computer and use it in GitHub Desktop.
Solve 4x4 sudoku in prolog stupidly
?- sudoku44(A1,2,3,A4,B1,B2,B3,2,1,C2,C3,C4,D1,4,1,D4).
1 2 3 4
-------------
A | . 2 | 3 . |
B | . . | . 2 |
-------------
C | 1 . | . . |
D | . 4 | 1 . |
-------------
A1 = B3, B3 = C4, C4 = 4,
A4 = B2, B2 = 1,
B1 = C2, C2 = D4, D4 = 3,
C3 = D1, D1 = 2
% I'm very new to prolog lol
unique(1,2,3,4).
unique(1,2,4,3).
unique(1,3,2,4).
unique(1,3,4,2).
unique(1,4,2,3).
unique(1,4,3,2).
unique(2,1,3,4).
unique(2,1,4,3).
unique(2,3,1,4).
unique(2,3,4,1).
unique(2,4,1,3).
unique(2,4,3,1).
unique(3,1,2,4).
unique(3,1,4,2).
unique(3,2,1,4).
unique(3,2,4,1).
unique(3,4,1,2).
unique(3,4,2,1).
unique(4,1,2,3).
unique(4,1,3,2).
unique(4,2,1,3).
unique(4,2,3,1).
unique(4,3,1,2).
unique(4,3,2,1).
sudoku44(A1,A2,A3,A4,B1,B2,B3,B4,C1,C2,C3,C4,D1,D2,D3,D4) :-
unique(A1,A2,A3,A4),
unique(B1,B2,B3,B4),
unique(C1,C2,C3,C4),
unique(D1,D2,D3,D4),
unique(A1,A2,B1,B2),
unique(A3,A4,B3,B4),
unique(C1,C2,D1,D2),
unique(C3,C4,D3,D4),
unique(A1,B1,C1,D1),
unique(A2,B2,C2,D2),
unique(A3,B3,C3,D3),
unique(A4,B4,C4,D4).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment