Skip to content

Instantly share code, notes, and snippets.

@Hamcha
Last active August 29, 2015 14:07
Show Gist options
  • Save Hamcha/f00d9d423fb9dd3fc694 to your computer and use it in GitHub Desktop.
Save Hamcha/f00d9d423fb9dd3fc694 to your computer and use it in GitHub Desktop.
% 0 - Not found | 1 - GG | 2 - GS
contains(_ ,_, [], _, 0).
contains(H, P, [H|_], P, 1).
contains(H, P, [H|_], Q, 2) :- P \== Q.
contains(H, P, [M|T], Q, O) :- H \== M, QQ is Q + 1, contains(H, P, T, QQ, O).
% Short version of contains/5
contains(H, P, L, O) :- contains(H, P, L, 1, O).
% Increase GG or GS depending on result
increase(0, GG, GS, GG, GS).
increase(1, GG, GS, GG + 1, GS).
increase(2, GG, GS, GG, GS + 1).
% Check two numbers and returns number of full/partial matches
check([], _, _, GG, GS, GG, GS).
check([HI|TI], I, O, GG, GS, GGG, GSS) :-
contains(HI, I, O, RES),
IX is I + 1,
increase(RES, GGG, GSS, GG2, GS2),
check(TI, IX, O, GG, GS, GG2, GS2).
% Short version of check/4
check(I, O, GG, GS) :- check(I, 1, O, GG, GS, 0, 0).
guess(I, O) :-
check(I, O, GG, GS),
format("~D gg ~D gs~n",[GG,GS]).
main :-
guess("1423","1243"),
guess("5143","5143"),
guess("9876","1234").
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment