forked from mmalita/PrologPuzzles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecretNumber.pl
46 lines (42 loc) · 1.49 KB
/
secretNumber.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* File: secretNumber.pl Author: Mihaela Malita
Title: Problem 60. Secrete Number I. Giant book of Mensa Mind Challenges
Sort of MASTERMIND.
You have to guess a SECRET = 5 digit integer (0-9) no repeats.
You know that the SECRET has in common with the following numbers (R (Right)
means one digit in common different place, B(Bingo) means one digit in common in the same place)
8951 - RR
2169 - RB
3694 - RB
4721 - RB
1237 - RRR
?- start(I).
[3,7,1,9]
*************************************************/
:- encoding(utf8).
:- use_module(library(lists)).
start(S) :-
length(S,4), % to check S=[3,7,1,9],
mem(S,[1,2,3,4,5,6,7,8,9]), is_set(S),
intersection([8,9,5,1],S,L1), length(L1,2), % 2 elements in common
intersection([2,1,6,9],S,L2), length(L2,2), samePlace(S,[2,1,6,9],[_]),
% 2 elements in common and 1 same position
intersection([3,6,9,4],S,L3), length(L3,2), samePlace(S,[3,6,9,4],[_]),
intersection([4,7,2,1],S,L4), length(L4,2), samePlace(S,[4,7,2,1],[_]),
intersection([1,2,3,7],S,L5), length(L5,3), % 3 elements in common
write(S).
/* Find elements the same in the same position
?- samePlace([a,b],[b,a],R).
R=[]
?- samePlace([a,b],[a,a],R).
false.
-----------------------------------------------------*/
samePlace([],[],[]).
samePlace([H|T1],[H|T2],[H|R]) :- samePlace(T1,T2,R).
samePlace([_|T1],[_|T2],R) :- samePlace(T1,T2,R).
/* mem(Lr,L). Elements from Lr are all members in L.
Or: Fill an empty list with elements from another list.
*/
mem([],_Y).
mem([H|T],Y) :-
member(H,Y),
mem(T,Y).