Author Topic: Felix's puzzle  (Read 21726 times)

Offline kido

  • SBC Old Boys
  • ***
  • Posts: 310
Re: Felix's puzzle
« Reply #10 on: 21 April 2011, 15:11:12 »
I have seen original question posted on other forum.... for completeness, here is my LAST attempt... which should solve this problem completely.... However, this is just kind of fun, don't take it seriously (ain't I?)  ;) ;)

Quote
parent_child(X,Y) :- father_child(X,Y).
parent_child(X,Y) :- mother_child(X,Y).

sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y), X\=Y.

mate(X,Y) :- father_child(X,Z), mother_child(Y,Z).

mate(X,Y) :- mother_child(X,Z), father_child(Y,Z).

son(X,Y) :-   parent_child(Y,X).

%%
%% Facts
%%

%% 2樓
mother_child(girlmom,girl).

%% 3樓
father_child(boy,girl).

%% 4樓
%%mother_child(girl, xxx).
%%father_child(boy, xxx).

%% 5樓
father_child(girldad,boy).
mother_child(boymom,boy).

%% 6樓
%% mate(boymom,girldad).

%% 7樓
sibling(boydad,girldad).

%% 8樓
%%mother_child(girlmom,boymom).
mother_child(boymom,girlmom).

%% 9樓
father_child(boydad,girlmom).

related(X,Y, 'mate') :- mate(X,Y).
related(X,Y, 'parent') :- parent_child(X,Y).
related(X,Y, 'sibling') :- sibling(X,Y).
related(X,Y, 'son') :- son(X,Y).

path(A,B,Path) :-
       travel(A,B,[A],Path).

travel(A,B,P,[B|P]) :-
       related(A,B,R),
      write(R), nl.
travel(A,B,Visited,Path) :-
      related(A,C,R),  
       C \= B,
       \+member(C,Visited),
       travel(C,B,[C|Visited],Path),
      write(R), nl.

Notes:

(1) Further thro' in relation 'son' (in fact son or daughter)

(2) Revamped relation 'related' again.

(3) Now the 'path' predicate should find ALL possible relation instead of just finding ONE solution as done previously.

Now run it again:

Quote
1 ?- path(boymom,girldad,P).
mate
P = [girldad, boymom] ;

sibling
mate
P = [girldad, boydad, boymom] ;

son
mate
parent
mate
P = [girldad, boy, girlmom, boydad, boymom] ;

son
son
parent
parent
mate
P = [girldad, boy, girl, girlmom, boydad, boymom] ;

son
sibling
parent
mate
P = [girldad, boy, girlmom, boydad, boymom] ;

son
parent
P = [girldad, boy, boymom] ;

sibling
son
mate
parent
P = [girldad, boydad, girlmom, boy, boymom] ;

sibling
son
son
parent
parent
P = [girldad, boydad, girlmom, girl, boy, boymom] ;

sibling
son
sibling
parent
P = [girldad, boydad, girlmom, boy, boymom] ;

son
mate
parent
P = [girldad, boy, girlmom, boymom] ;

son
son
parent
parent
P = [girldad, boy, girl, girlmom, boymom] ;

son
sibling
parent
P = [girldad, boy, girlmom, boymom] ;

sibling
son
parent
P = [girldad, boydad, girlmom, boymom] ;

false.

The program shows that there are > 10 relations between them.... To name them.... I will leave to anyone who are interested.
Hey, diddle, diddle ! The cat and the fiddle.

Offline chin

  • Global Moderator
  • *****
  • Posts: 6,663
Re: Felix's puzzle
« Reply #11 on: 21 April 2011, 17:41:31 »
And the answer to the last question?

Offline kido

  • SBC Old Boys
  • ***
  • Posts: 310
Re: Felix's puzzle
« Reply #12 on: 21 April 2011, 18:28:11 »
Oh, yeah, I thought it was asking 男友他的媽見到女友的爸  instead of  男友他媽的媽見到女友他媽的爸, because "他媽的" is not part of the relation....

But anyway, now I throw in new relation :

And to makes things more complicated, now I put back the "xxx".

Quote
mother_child(boymm,boymom).
father_child(girlmd,girlmom).

And run it:

Quote
1 ?- path(boymm,girlmd,P).
mate
parent
P = [girlmd, boymom, boymm] ;

son
mate
parent
mate
parent
P = [girlmd, girlmom, boy, girldad, boymom, boymm] ;

son
son
mate
parent
mate
parent
P = [girlmd, girlmom, girl, boy, girldad, boymom, boymm] ;

son
son
parent
parent
mate
parent
P = [girlmd, girlmom, girl, boy, girldad, boymom, boymm] ;

son
son
sibling
parent
parent
mate
parent
P = [girlmd, girlmom, girl, xxx, boy, girldad, boymom, boymm] ;

son
son
son
parent
parent
mate
parent
P = [girlmd, girlmom, girl, xxx, boy, girldad, boymom, boymm] ;

son
sibling
parent
mate
parent
P = [girlmd, girlmom, boy, girldad, boymom, boymm] ;

son
parent
mate
parent
P = [girlmd, girlmom, boydad, boymom, boymm] ;

son
mate
parent
sibling
mate
parent
P = [girlmd, girlmom, boy, girldad, boydad, boymom, boymm] ;

son
son
mate
parent
sibling
mate
parent
P = [girlmd, girlmom, girl, boy, girldad, boydad, boymom, boymm] ;

son
son
parent
parent
sibling
mate
parent
P = [girlmd, girlmom, girl, boy, girldad, boydad, boymom, boymm] ;

son
son
sibling
parent
parent
sibling
mate
parent
P = [girlmd, girlmom, girl, xxx, boy, girldad, boydad, boymom, boymm] ;

son
son
son
parent
parent
sibling
mate
parent
P = [girlmd, girlmom, girl, xxx, boy, girldad, boydad, boymom, boymm] ;

son
sibling
parent
sibling
mate
parent
P = [girlmd, girlmom, boy, girldad, boydad, boymom, boymm] ;

son
mate
parent
parent
P = [girlmd, girlmom, boy, boymom, boymm] ;

son
son
mate
parent
parent
P = [girlmd, girlmom, girl, boy, boymom, boymm] ;

son
son
parent
parent
parent
P = [girlmd, girlmom, girl, boy, boymom, boymm] ;

son
son
sibling
parent
parent
parent
P = [girlmd, girlmom, girl, xxx, boy, boymom, boymm] ;

son
son
son
parent
parent
parent
P = [girlmd, girlmom, girl, xxx, boy, boymom, boymm] ;

son
sibling
parent
parent
P = [girlmd, girlmom, boy, boymom, boymm] ;

son
parent
parent
P = [girlmd, girlmom, boymom, boymm] ;

false.


Hey, diddle, diddle ! The cat and the fiddle.

Offline kido

  • SBC Old Boys
  • ***
  • Posts: 310
Re: Felix's puzzle
« Reply #13 on: 21 April 2011, 18:34:10 »
I think it's just for illustration, perhaps the only human-understandable relation is the one below:

Quote
1 ?- path(boymm,girlmd,P).
mate
parent
P = [girlmd, boymom, boymm] ;
Hey, diddle, diddle ! The cat and the fiddle.