Created
June 30, 2012 16:00
-
-
Save jmora/3024369 to your computer and use it in GitHub Desktop.
This is a prolog sudoku solver I coded years ago and found back minutes ago.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
author('@j_mora'). | |
:- use_package(clpq). | |
numbers([1,2,3,4,5,6,7,8,9]). | |
myMember(E, [E|Ls], Ls). | |
myMember(E, [L|Ls], [L|Rs]):- myMember(E, Ls, Rs). | |
sudoku(X):- numbers(Ns), squares(X), columns(X), rows(X, Ns). | |
rows([], _). | |
rows([R|Rs], Ns):- row(R, Ns), rows(Rs, Ns). | |
row([],[]). | |
row([R|Rs], Ns):- myMember(R, Ns, RNs), row(Rs, RNs). | |
columns([[]|_]). | |
columns(Cs):- column(C, Cs, RCs), columns(RCs), restrict(C). | |
column([E],[[E|R]],[R]). | |
column([C|RC], [[C|Cs]|Ms], [Cs|RM]):- column(RC, Ms, RM). | |
squares([]). | |
squares(X):- square(X, S, RX), restrict(S), squares(RX). | |
square([[A1, A2, A3], [B1, B2, B3], [C1, C2, C3]|RR], | |
[A1, A2, A3, B1, B2, B3, C1, C2, C3], RR). | |
square([[A1, A2, A3|R1], [B1, B2, B3|R2], [C1, C2, C3|R3]|RR], | |
[A1, A2, A3, B1, B2, B3, C1, C2, C3], | |
[R1, R2, R3|RR]). | |
restrict([]). | |
restrict([L|Ls]):- restrictElement(L, Ls), restrict(Ls). | |
restrictElement(_, []). | |
restrictElement(E, [L|Ls]):- E .<>. L, restrictElement(E, Ls). | |
unify(X, X). | |
test1(X):- unify(X, | |
[[_, 6, _, _, 2, _, _, _, _], | |
[7, _, _, 8, 4, _, _, _, 6], | |
[_, 5, _, 1, _, _, 7, _, _], | |
[3, _, 8, _, _, _, 1, _, _], | |
[_, _, _, _, _, _, _, _, _], | |
[_, _, 1, _, _, _, 2, _, 4], | |
[_, _, 6, _, _, 2, _, 4, _], | |
[_, _, _, _, 7, 1, _, _, 9], | |
[_, _, _, _, 9, _, _, 3, _]]), | |
sudoku(X). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment