Skip to content

Instantly share code, notes, and snippets.

@forestbelton
Last active March 26, 2020 10:27
Show Gist options
  • Save forestbelton/d5233264bb6db195f29a605bd6f41ef5 to your computer and use it in GitHub Desktop.
Save forestbelton/d5233264bb6db195f29a605bd6f41ef5 to your computer and use it in GitHub Desktop.
% A keypad lock on a door has a code of five different digits. Can you use the six clues below to determine the correct code to get in?
%
% 1. There are no zeroes in the five-digit code.
% 2. The first digit is smaller than the second digit.
% 3. Exactly two of the five digits are odd.
% 4. The middile digit is a perfect square.
% 5. The two-digit number formed by the fourth and fifth digits is a perfect square.
% 6. The two-digit number formed by the third and fourth digits is equal to the first digit multiplied by ythe second digit.
is_digit(1).
is_digit(2).
is_digit(3).
is_digit(4).
is_digit(5).
is_digit(6).
is_digit(7).
is_digit(8).
is_digit(9).
is_perfect_square(X) :- is_digit(T), X is T * T.
odd(1).
odd(N) :- N > 0, K is N - 2, odd(K).
two_odd([A,B,_,_,_]) :- odd(A), odd(B).
two_odd([A,_,C,_,_]) :- odd(A), odd(C).
two_odd([A,_,_,D,_]) :- odd(A), odd(D).
two_odd([A,_,_,_,E]) :- odd(A), odd(E).
two_odd([_,B,C,_,_]) :- odd(B), odd(C).
two_odd([_,B,_,D,_]) :- odd(B), odd(D).
two_odd([_,B,_,_,E]) :- odd(B), odd(E).
two_odd([_,_,C,D,_]) :- odd(C), odd(D).
two_odd([_,_,C,_,E]) :- odd(C), odd(E).
two_odd([_,_,_,D,E]) :- odd(D), odd(E).
all_diff([A,B,C,D,E]) :-
A \= B, A \= C, A \= D, A \= E,
B \= C, B \= D, B \= E,
C \= D, C \= E,
D \= E.
solution(Code) :-
Code = [A,B,C,D,E],
is_digit(A),
is_digit(B),
is_digit(C),
is_digit(D),
is_digit(E),
all_diff(Code),
A < B,
two_odd(Code),
C \= 1,
is_perfect_square(C),
P0 is D * 10 + E,
is_perfect_square(P0),
P1 is A * B,
P1 is C * 10 + D.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment