Δευτέρα 5 Μαΐου 2008

prolog askish 1 lush...

% η λύση είναι ενδεικτική - προφανώς μπορεί να γίνει με διάφορους τρόπους

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 6-ades apo [21,22,23,24,25,26,27,28,29,30] --- 210 synolika %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

s([21,22,23,24,25,26]).
s([22,23,24,25,26,27]).
s([23,24,25,26,27,28]).
s([24,25,26,27,28,29]).
s([25,26,27,28,29,30]).
s([21,22,23,24,25,27]).
s([22,23,24,25,26,28]).
s([23,24,25,26,27,29]).
s([24,25,26,27,28,30]).
s([21,22,23,24,25,28]).
s([22,23,24,25,26,29]).
s([23,24,25,26,27,30]).
s([21,22,23,24,25,29]).
s([22,23,24,25,26,30]).
s([21,22,23,24,25,30]).
s([21,22,23,24,26,27]).
s([22,23,24,25,27,28]).
s([23,24,25,26,28,29]).
s([24,25,26,27,29,30]).
s([21,22,23,24,26,28]).
s([22,23,24,25,27,29]).
s([23,24,25,26,28,30]).
s([21,22,23,24,26,29]).
s([22,23,24,25,27,30]).
s([21,22,23,24,26,30]).
s([21,22,23,24,27,28]).
s([22,23,24,25,28,29]).
s([23,24,25,26,29,30]).
s([21,22,23,24,27,29]).
s([22,23,24,25,28,30]).
s([21,22,23,24,27,30]).
s([21,22,23,24,28,29]).
s([22,23,24,25,29,30]).
s([21,22,23,24,28,30]).
s([21,22,23,24,29,30]).
s([21,22,23,25,26,27]).
s([22,23,24,26,27,28]).
s([23,24,25,27,28,29]).
s([24,25,26,28,29,30]).
s([21,22,23,25,26,28]).
s([22,23,24,26,27,29]).
s([23,24,25,27,28,30]).
s([21,22,23,25,26,29]).
s([22,23,24,26,27,30]).
s([21,22,23,25,26,30]).
s([21,22,23,25,27,28]).
s([22,23,24,26,28,29]).
s([23,24,25,27,29,30]).
s([21,22,23,25,27,29]).
s([22,23,24,26,28,30]).
s([21,22,23,25,27,30]).
s([21,22,23,25,28,29]).
s([22,23,24,26,29,30]).
s([21,22,23,25,28,30]).
s([21,22,23,25,29,30]).
s([21,22,23,26,27,28]).
s([22,23,24,27,28,29]).
s([23,24,25,28,29,30]).
s([21,22,23,26,27,29]).
s([22,23,24,27,28,30]).
s([21,22,23,26,27,30]).
s([21,22,23,26,28,29]).
s([22,23,24,27,29,30]).
s([21,22,23,26,28,30]).
s([21,22,23,26,29,30]).
s([21,22,23,27,28,29]).
s([22,23,24,28,29,30]).
s([21,22,23,27,28,30]).
s([21,22,23,27,29,30]).
s([21,22,23,28,29,30]).
s([21,22,24,25,26,27]).
s([22,23,25,26,27,28]).
s([23,24,26,27,28,29]).
s([24,25,27,28,29,30]).
s([21,22,24,25,26,28]).
s([22,23,25,26,27,29]).
s([23,24,26,27,28,30]).
s([21,22,24,25,26,29]).
s([22,23,25,26,27,30]).
s([21,22,24,25,26,30]).
s([21,22,24,25,27,28]).
s([22,23,25,26,28,29]).
s([23,24,26,27,29,30]).
s([21,22,24,25,27,29]).
s([22,23,25,26,28,30]).
s([21,22,24,25,27,30]).
s([21,22,24,25,28,29]).
s([22,23,25,26,29,30]).
s([21,22,24,25,28,30]).
s([21,22,24,25,29,30]).
s([21,22,24,26,27,28]).
s([22,23,25,27,28,29]).
s([23,24,26,28,29,30]).
s([21,22,24,26,27,29]).
s([22,23,25,27,28,30]).
s([21,22,24,26,27,30]).
s([21,22,24,26,28,29]).
s([22,23,25,27,29,30]).
s([21,22,24,26,28,30]).
s([21,22,24,26,29,30]).
s([21,22,24,27,28,29]).
s([22,23,25,28,29,30]).
s([21,22,24,27,28,30]).
s([21,22,24,27,29,30]).
s([21,22,24,28,29,30]).
s([21,22,25,26,27,28]).
s([22,23,26,27,28,29]).
s([23,24,27,28,29,30]).
s([21,22,25,26,27,29]).
s([22,23,26,27,28,30]).
s([21,22,25,26,27,30]).
s([21,22,25,26,28,29]).
s([22,23,26,27,29,30]).
s([21,22,25,26,28,30]).
s([21,22,25,26,29,30]).
s([21,22,25,27,28,29]).
s([22,23,26,28,29,30]).
s([21,22,25,27,28,30]).
s([21,22,25,27,29,30]).
s([21,22,25,28,29,30]).
s([21,22,26,27,28,29]).
s([22,23,27,28,29,30]).
s([21,22,26,27,28,30]).
s([21,22,26,27,29,30]).
s([21,22,26,28,29,30]).
s([21,22,27,28,29,30]).
s([21,23,24,25,26,27]).
s([22,24,25,26,27,28]).
s([23,25,26,27,28,29]).
s([24,26,27,28,29,30]).
s([21,23,24,25,26,28]).
s([22,24,25,26,27,29]).
s([23,25,26,27,28,30]).
s([21,23,24,25,26,29]).
s([22,24,25,26,27,30]).
s([21,23,24,25,26,30]).
s([21,23,24,25,27,28]).
s([22,24,25,26,28,29]).
s([23,25,26,27,29,30]).
s([21,23,24,25,27,29]).
s([22,24,25,26,28,30]).
s([21,23,24,25,27,30]).
s([21,23,24,25,28,29]).
s([22,24,25,26,29,30]).
s([21,23,24,25,28,30]).
s([21,23,24,25,29,30]).
s([21,23,24,26,27,28]).
s([22,24,25,27,28,29]).
s([23,25,26,28,29,30]).
s([21,23,24,26,27,29]).
s([22,24,25,27,28,30]).
s([21,23,24,26,27,30]).
s([21,23,24,26,28,29]).
s([22,24,25,27,29,30]).
s([21,23,24,26,28,30]).
s([21,23,24,26,29,30]).
s([21,23,24,27,28,29]).
s([22,24,25,28,29,30]).
s([21,23,24,27,28,30]).
s([21,23,24,27,29,30]).
s([21,23,24,28,29,30]).
s([21,23,25,26,27,28]).
s([22,24,26,27,28,29]).
s([23,25,27,28,29,30]).
s([21,23,25,26,27,29]).
s([22,24,26,27,28,30]).
s([21,23,25,26,27,30]).
s([21,23,25,26,28,29]).
s([22,24,26,27,29,30]).
s([21,23,25,26,28,30]).
s([21,23,25,26,29,30]).
s([21,23,25,27,28,29]).
s([22,24,26,28,29,30]).
s([21,23,25,27,28,30]).
s([21,23,25,27,29,30]).
s([21,23,25,28,29,30]).
s([21,23,26,27,28,29]).
s([22,24,27,28,29,30]).
s([21,23,26,27,28,30]).
s([21,23,26,27,29,30]).
s([21,23,26,28,29,30]).
s([21,23,27,28,29,30]).
s([21,24,25,26,27,28]).
s([22,25,26,27,28,29]).
s([23,26,27,28,29,30]).
s([21,24,25,26,27,29]).
s([22,25,26,27,28,30]).
s([21,24,25,26,27,30]).
s([21,24,25,26,28,29]).
s([22,25,26,27,29,30]).
s([21,24,25,26,28,30]).
s([21,24,25,26,29,30]).
s([21,24,25,27,28,29]).
s([22,25,26,28,29,30]).
s([21,24,25,27,28,30]).
s([21,24,25,27,29,30]).
s([21,24,25,28,29,30]).
s([21,24,26,27,28,29]).
s([22,25,27,28,29,30]).
s([21,24,26,27,28,30]).
s([21,24,26,27,29,30]).
s([21,24,26,28,29,30]).
s([21,24,27,28,29,30]).
s([21,25,26,27,28,29]).
s([22,26,27,28,29,30]).
s([21,25,26,27,28,30]).
s([21,25,26,27,29,30]).
s([21,25,26,28,29,30]).
s([21,25,27,28,29,30]).
s([21,26,27,28,29,30]).

% ---- PROSOXH - PARADOXES --------------------------
% se opoies periptwseis prepei na dwthei lista 6
% arithmwn apo to xrhsth, oi arithmoi prepei na
% dinontai se au3ousa seira. p.x. [21,23,24,25,29,30]
% ---------------------------------------------------



%=======================================================
%=1= ODD FILTER ========================================
%=======================================================

% (i,o) Returns the number of odd numbers in list
%----------------------------------------------------
count_odd([],0).
count_odd([H|T],ODDCount):-
count_odd(T,E),
C is H mod 2, %if C=1 then H is odd
ODDCount is E+C.

% (i,i) succeeds if LIST has ODDs odd numbers
%----------------------------------------------------
odd_filter(ODDs,LIST):-
count_odd(LIST,ODDCount),
ODDCount=ODDs.



%=======================================================
%=2= SMALL FILTER ======================================
%=======================================================

% (i,o) returns 1 if X<25,>=25
%---------------------------------------------------
is_small(X,1):- X<25.>=25.

% (i,o) SMALLCount is the number of small numbers in list
%----------------------------------------------------
count_small([],0).
count_small([H|T],SMALLCount):-
count_small(T,B),
is_small(H,C),
SMALLCount is B+C.

% (i,i) succeeds if list has SMALLCount small numbers
%----------------------------------------------------
small_filter(SMALLs,LIST):-
count_small(LIST,SMALLCount),
SMALLCount=SMALLs.




%=======================================================
%=3= SUCCESSIVE FILTER =================================
%=======================================================

% (i) succeeds if list has 6 successive numbers
%----------------------------------------------------
successive6([N1,N2,N3,N4,N5,N6]):-
N2-N1=:=1, N3-N2=:=1, N4-N3=:=1, N5-N4=:=1, N6-N5=:=1.

% (i) succeeds if list does not contain 6 successive numbers
%----------------------------------------------------
successive_filter(no,_).
successive_filter(yes,LIST):- not(successive6(LIST)).



%=======================================================
%=4= FILTRARISMA SYNOLIKA ==============================
%=======================================================
filter(ODDs,SMALLs,SUCCESSIVE,LIST):-
s(LIST), % recalls all s/1 terms through backtracking
odd_filter(ODDs,LIST), % succeeds if LIST has ODDs odd numbers
small_filter(SMALLs,LIST), % succeeds if LIST has SMALLs small numbers
successive_filter(SUCCESSIVE,LIST). % succeeds if SUCCESSIVE=yes and LIST does
% not contain 6 successive numbers




%=======================================================
%=5= DIALOGH ==========================================
%=======================================================

% (i,i) - prints a proper message according to the success
%------------------------------------------------------------
display_results(LIST, 4):- write(LIST), write(' τεσσάρι!'), nl.
display_results(LIST, 5):- write(LIST), write(' πεντάρι!'), nl.
display_results(LIST, 6):- write(LIST), write(' εξάρι!'), nl.


% (i,i,o) - the 3rd list is the intersection of the 1st and 2nd lists
%------------------------------------------------------------
intersect([], _, []).
intersect([H1|T1], LIST, [H1|T2]):-
member(H1,LIST),
intersect(T1,LIST,T2).
intersect([H1|T1], LIST, L2):-
not(member(H1,LIST)), % anti gia not mporei na mpei "cut" meta
% to member tou prohgoumenou clause
intersect(T1,LIST,L2).


% (i) - WinnList is the winning list
% compares stored lists against WinnList and displays
% proper messages if 4 or more numbers were guessed.
%------------------------------------------------------------
dialogh(WinnList):-
s(LIST), % recalls all s/1 terms through backtracking
intersect(WinnList, LIST, Common), % intersects WinnList with LIST to fing the common elements
length(Common,Len),
Len>3,
display_results(LIST, Len), % displays some messages when LIST wins
fail;true. % e3anagkasmos se backtracking gia e3antlhsh twn s(LIST)



% το go παρακάτω περιέχει διάφορες κλίσεις για τον έλεγχο ότι
% όλα λειτουργούν όπως πρέπει.
go:-
odd_filter(3,[21,23,24,25,26,28]),
not(odd_filter(2,[22,24,25,26,27,29])),
write(' odd OK '), nl,
not(small_filter(2,[21,23,24,25,26,28])),
small_filter(3,[21,23,24,25,26,28]),
write(' small OK '), nl,
successive_filter(no,_),
not(successive_filter(yes,[21,22,23,24,25,26])),
successive_filter(yes,[21,22,23,24,25,27]),
write(' suc OK '), nl,
filter(3,3,no,[22,23,24,25,26,27]),
not(filter(3,3,yes,[22,23,24,25,26,27])),
write(' all 1 OK '), nl, !,
dialogh([22,24,25,26,27,30]),
write(' DIALOGH OK '), nl, !,
filter(2,1,yes,X),
write(X), nl,
fail.