Skip to content

Instantly share code, notes, and snippets.

@fferri
Created December 1, 2018 11:08
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 fferri/787d5342ac3b9cb4128b0e6b42f8918b to your computer and use it in GitHub Desktop.
Save fferri/787d5342ac3b9cb4128b0e6b42f8918b to your computer and use it in GitHub Desktop.
correct_position(A, B, C, S) :- S = [A, _, _], \+ member(B, S), \+ member(C, S).
correct_position(A, B, C, S) :- S = [_, B, _], \+ member(A, S), \+ member(C, S).
correct_position(A, B, C, S) :- S = [_, _, C], \+ member(A, S), \+ member(B, S).
wrong_position(A, B, C, S) :- (S = [_, A, _] ; S = [_, _, A]), \+ member(B, S), \+ member(C, S).
wrong_position(A, B, C, S) :- (S = [B, _, _] ; S = [_, _, B]), \+ member(A, S), \+ member(C, S).
wrong_position(A, B, C, S) :- (S = [C, _, _] ; S = [_, C, _]), \+ member(A, S), \+ member(B, S).
two_wrong_position(A, B, C, S) :- S \= [A, _, _], S \= [_, B, _], member(A, S), member(B, S), \+ member(C, S).
two_wrong_position(A, B, C, S) :- S \= [A, _, _], S \= [_, _, C], member(A, S), member(C, S), \+ member(B, S).
two_wrong_position(A, B, C, S) :- S \= [_, B, _], S \= [_, _, C], member(B, S), member(C, S), \+ member(A, S).
none(A, B, C, S) :- \+ member(A, S), \+ member(B, S), \+ member(C, S).
solution(S) :-
between(0, 9, A), between(0, 9, B), between(0, 9, C), S = [A, B, C],
correct_position(6, 8, 2, S),
wrong_position(6, 4, 5, S),
two_wrong_position(2, 0, 6, S),
none(7, 3, 8, S),
wrong_position(7, 8, 0, S).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment