Has anyone got the Five Men, Five Houses, Five Pets... Problem in Prolog? 
Author Message
 Has anyone got the Five Men, Five Houses, Five Pets... Problem in Prolog?

Has anyone got the Five Men, Five Houses, Five Pets... Problem in Prolog?

James.



Mon, 03 Sep 2001 03:00:00 GMT  
 Has anyone got the Five Men, Five Houses, Five Pets... Problem in Prolog?

Quote:
>Has anyone got the Five Men, Five Houses, Five Pets... Problem in Prolog?

Yes, the problem is given below.

There are five houses, each of a different color and inhabited by men of
different nationalities, with different pets, drinks and brands of
cigarettes. Given the following clues, determine who drinks water and who
owns the zebra.

Clues:
1. The Englishman lives in the red house.
2. The Spaniard owns the dog.
3. The Norwegian lives in the first house on the left.
4. Kools are smoked in the yellow house.
5. The man who smokes Chesterfields lives in the house next to the man with
the fox.
6. The Norwegian lives next to the blue house.
7. The Winston smoker owns snails.
8. The Lucky Strike smoker drinks orange juice.
9. The Ukranian drinks tea.
10. The Japanese smokes Parliaments.
11. Kools are smoked in the house next to the house where the horse is kept.
12. Coffee is drunk in the green house.
13. The green house is immediately to the right (your right) of the ivory
house.
14. Milk is drunk in the middle house.

Daniel



Mon, 03 Sep 2001 03:00:00 GMT  
 Has anyone got the Five Men, Five Houses, Five Pets... Problem in Prolog?
For a solution in Prolog, have a look at
http://perso.wanadoo.fr/colin.barker/lpa/puzzle.htm
--
Colin


Internet: http://perso.wanadoo.fr/colin.barker

Daniel Dudley a crit dans le message ...

Quote:

>>Has anyone got the Five Men, Five Houses, Five Pets... Problem in Prolog?

<snip>



Mon, 03 Sep 2001 03:00:00 GMT  
 Has anyone got the Five Men, Five Houses, Five Pets... Problem in Prolog?
Note, Colin, that the request was for the PROBLEM, not the solution.

Daniel

Quote:

>For a solution in Prolog, have a look at
>http://perso.wanadoo.fr/colin.barker/lpa/puzzle.htm
>--
>Colin


>Internet: http://perso.wanadoo.fr/colin.barker

>Daniel Dudley a crit dans le message ...

>>>Has anyone got the Five Men, Five Houses, Five Pets... Problem in Prolog?

><snip>



Mon, 03 Sep 2001 03:00:00 GMT  
 Has anyone got the Five Men, Five Houses, Five Pets... Problem in Prolog?
Daniel,

I'm glad you replied to this posting.  My newsgroup server didn't pick up
the intermediate message.  Without yours I wouldn't have gotten the address.

Thanks.

James.

Quote:

>Note, Colin, that the request was for the PROBLEM, not the solution.



Fri, 07 Sep 2001 03:00:00 GMT  
 Has anyone got the Five Men, Five Houses, Five Pets... Problem in Prolog?
Hum, how to say that a yes: HERE IT IS!!!!!!!!!

First one found with deja news
/*

                    > When I first saw this puzzle, I thought to myself:
                    > "This is the time to have Prolog and run for the
                    solution.."
                    > But somehow I was stock and couldn't construct the
                    program that
                    > was able to answer the question. Is there anyone
out
                    there interested
                    > in this puzzle, and would like to give it a try?
                    >
                    > The puzzle:
                    > Assumptions -
                    >  1) five houses in a row
                    >  2) every house is painted with different color
                    >  3) every house is owned by different people
                    >  4) every house owner has different kind of pet,
                    >      drinks different beverage, and smokes
different
                    brand
                    >      of cigarette.
                    >
                    >
                    > Hints  [...]
                    >
                    > Question: Who has fish?

                    The same puzzle, with a few minor modifications
(only 14
                    constraints, car types instead of cigarette brands,
other
                    nationalities,  was given as an exercise in a course
I had
                    this spring.  The following program should solve
it.  For
                    those who can't be bothered to run the program, the
                    solution is:

                    Nation    No.    Color  Pet    Drink  Smoke

-----------------------------------------------------
                    Norwegian  1      yellow  cat    water  Dunhill
                    Danish    2      blue    horse  tea    Blend
                    British    3      red    bird    milk    Pallmall
                    German    4      green  fish    coffee  Prince
                    Swedish    5      white  dog    beer    Bluemaster

                    The german owns the fish.

H = [   person(norwegian,1,yellow,cat,water,dunhill),
        person(danish,2,blue,horse,tea,blend),
        person(british,3,red,bird,milk,pallmll),
        person(german,4,green,fish,coffee,prince),
        person(swedish,5,white,dog,beer,bluemaster)]

*/
%%% fish.prolog
%%%
%%% The program uses the functor
%%% person(Nationality, HouseNo, HouseColor, Pet, Drink, Smoke)
%%% to describe who has which house.

solve(P) :-
        test(P),
        generate(P).

test(P) :-
        P = [person(_,1,_,_,_,_),
                person(_,2,_,_,_,_),
                        person(_,3,_,_,_,_),            
                person(_,4,_,_,_,_),
                        person(_,5,_,_,_,_)],

        member(person(british, _, red, _, _, _), P), %  Constraint 1        
        member(person(swedish, _, _, dog, _, _), P), % Constraint 2        
        member(person(danish, _, _, _, tea, _), P), % Constraint 3        
        pick(person(_, C5a, green, _, _, _), P, C5rest), % Constraint 4        
        member(person(_, C5b, white, _, _, _), C5rest), 1 is C5b  - C5a,
        member(person(_, _, green, _, coffee, _), P), % Constraint 5        
        member(person(_, _, _, bird, _, pallmall), P), % Constraint 6        
        member(person(_, _,yellow, _, _, dunhill), P), % Constraint 7        
        member(person(norwegian, 1, _, _, _, _), P), %  Constraint 8        
        member(person(_, 3, _, _, milk, _), P),  % Constraint 9
        pick(person(_, C10a, _, _, _, blend), P, C10rest), % Constraint
10        
        member(person(_, C10b, _, cat, _, _),  C10rest),
        nextto(C10a, C10b),
        pick(person(_, C11a, _, _, _, dunhill), P, C11rest),  % Constraint 11
        member(person(_, C11b, _, horse, _, _), C11rest),
        nextto(C11a, C11b),
        member(person(_, _, _, _, beer, bluemaster), P), % Constraint
12        
        member(person(german, _, _, _, _, prince), P), % Constraint 13        
        pick(person(norwegian,C14a, _, _, _, _), P, C14rest), % Constraint
14        
        member(person(_, C14b, blue, _, _, _), C14rest),
        nextto(C14a, C14b),
        pick(person(_, C15a, _, _, _, blend), P, C15rest), % Constraint
15        
        member(person(_, C15b, _, _, water,  _), C15rest),
        nextto(C15a, C15b).                    
        generate([person(N1, HN1, HC1, P1, D1, S1),
        person(N2, HN2, HC2, P2, D2, S2),
        person(N3, HN3, HC3, P3, D3, S3),
        person(N4, HN4, HC4, P4, D4, S4),
        person(N5, HN5, HC5, P5, D5, S5)]) :-
        permutation([N1, N2, N3, N4, N5],
        [british, norwegian, german, danish, swedish]),
        permutation([HN1, HN2, HN3, HN4, HN5],[1,2,3,4,5]),
        permutation([HC1, HC2, HC3, HC4, HC5],
        [red, green, white, yellow, blue]),
        permutation([P1, P2, P3, P4, P5],
        [dog, bird, cat, horse, fish]),
        permutation([D1, D2, D3, D4, D5],
        [coffee, tea, milk, beer, water]),
        permutation([S1, S2, S3, S4, S5],
        [pallmall, dunhill, blend, prince, bluemaster]).

pick(X, [X|Xs], Xs).
      pick(X, [Y|Ys], [Y|Zs]) :-
      pick(X, Ys, Zs).

permutation([], []).
permutation([X|Xs], Y) :-
        pick(X, Y, Ys),
        permutation(Xs, Ys).

member(X, [X|_]).
member(X, [_|Y]) :- member(X, Y).

nextto(A, B) :-
        X is A-B,
        1 is X * X.

?- solve(T),
   write( T ), nl.

%%%%%%%%%%%%%
%%%%%%%%%%%%%
%%%%%%%%%%%%%%      %%
%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
The last from my little brain
/*
Le zbre

Cinq maisons conscutives, de couleurs diffrentes, sont habites par
des hommes de
diffrentes nationalits.Ils possdent tous un animal diffrent, ont
chacun une
boisson prfe diffrente et fument des cigarettes diffrentes.
On sait que:
 1) Le norvgien habite la 1er maison
 2) La maison cot de celle du norvgien est bleu
 3) L'habitant de la 3eme maison boit du lait
 4) L'anglais habite la maison rouge
 5) L'habitant de la maison verte bois du caf
 6) L'habitant de la maison jaune fume des kool
 7) La maison blanche se trouve juste apres la verte
 8) L'espagnol a un chien
 9) L'ukranien boit du th
10) Le japonais fume des craven
11) Le fumeur de old gold a un escargot
12) Le fumeur de gitane boit du vin
13) Un voisin du fumeur de chesterfield a un renard
14) Un voisin du fumeur de kool a un cheval

Questions: Qui boit de l'eau? Qui a un zbre?
*/

/* Prolog resolution par patrick Hede Aout 1999 */

/* 1) Base de connaissance */
base( Lx ) :-
        Couleur=[verte,bleu,rouge,jaune,blanche],
        Animal=[ chien,escargot,renard,cheval,zebre],
        Boison=[ cafe, the,lait, vin,eau],      
        Fume=[ gitane,craven,old_gold,kool,chesterfeild],
        Nation=[ espagnol,anglais,norvegien,ukranien,japonais],
        maisons( Couleur, Animal, Boison, Fume, Nation ),
        Lx is 1,
nop.

/* 2) Code begin  */
nop.                    % Instruction essentielle qui ne realise rien!

/* Un peu de manipulation de liste */
% lnelem/3              lnelem(Lin:list, XinXout:atom, NinNout:atom )
%                       Retourne a partir de la Liste L l'element X et/ou son rang suivant
l'ordre des variables instancies.
lnelem(L, X, N) :-
        lnelem(L, X, 1, N).
lnelem([X|Z], X, N, N).
lnelem([_|Z], X, A, N) :-
    AA is A + 1,
lnelem(Z, X, AA, N).  

% ltakeout/3             ltakeout( atom, list, list )
%                       En general efface un element d'une liste, mais il est possible
d'inserer
%                       un element suivant l'ordre des variables instancies.
ltakeout(X,[X|R],R).
ltakeout(X,[F|R],[F|S]) :- ltakeout(X,R,S).

%lperm/2                        Effectue toute les permutations sur une liste n!
%                       typical call: lpemr([1,2,3], K ).
lperm([X|Y],Z) :- lperm(Y,W), ltakeout(X,Z,W).
lperm([],[]).

/*% protoype maisons( COULEUR[], ANIMAL[], BOISONS[], FUME[],
NATIONAMITE[]). maisons/5 */
maisons( C, A, B, F, N ) :-

% Generator
        %+lperm( C, Lc ), +lperm( A, La ), +lperm( B, Lb ), +lperm( F, Lf ),
+lperm( N, Ln ),

% Tester

        lperm( N, Ln ),
        % 1) Le norvgien habite la 1er maison        
        lnelem( Ln, norvegien, 1),      %write( 'Contrainte 1 satisfaite: '),
write( Ln ), nl,

        lperm( C, Lc ),
        % 7) La maison blanche se trouve juste apres la verte  
        lnelem( Lc, verte, Pv ), Pb is Pv+1,
        lnelem( Lc, blanche, Pb ),      %write( 'Contrainte 7 satisfaite: '), write(
Ln ), nl,

        % 2) La maison cot de celle du norvgien est bleu
        lnelem( Ln, norvegien, Pnor ),  
        PosD is Pnor+1,                 % Out of range don't care. En effet la clause est
insatisafaite d'elle meme!      
        PosG is Pnor-1,
        % rappel dit le point virgule ; je suis un OU inclusif
        % rappel crie la virgule , je suis le ET
        (lnelem(Lc, bleu, PosD) ; lnelem( Lc, bleu, PosG )),
                                %write( 'Contrainte 2 satisfaite: '), write( Ln ), nl,

        % 4) L'anglais habite la maison rouge  
        lnelem( Ln, anglais, Pa ),
        lnelem( Lc, rouge, Pa ),        %write( 'Contrainte 4 satisfaite: '), write(
Lc ), nl,

%%
        lperm( B, Lb ),
        % 3) L'habitant de la 3eme maison boit du lait  
        lnelem( Lb, lait, 3 ),          %write( 'Contrainte 3 satisfaite: '), write(
Lb ), nl,

        % 5) L'habitant de la maison verte bois du caf        
        lnelem( Lc, verte, Pv ),
        lnelem( Lb, cafe, Pv ), %write( 'Contrainte 5 satisfaite: '), write( Lb
), nl,

        % 9) L'ukranien boit du th    
        lnelem( Ln, ukranien, Pu ),
        lnelem( Lb, the, Pu ),  %write( 'Contrainte 9 satisfaite: '), write( Lb
), nl,

%%%
        lperm( F,  Lf),
        % 6) L'habitant de la maison jaune fume des kool        
        lnelem( Lc, jaune, Pjaune ),
        lnelem( Lf, kool, Pjaune ),     %write( 'Contrainte 6 satisfaite: '),
write( Lf ), nl,

        % 10) Le japonais fume des craven
        lnelem( Ln, japonais, Pjapon ),
        lnelem( Lf, craven, Pjapon ),   %write( 'Contrainte 10 satisfaite: '),
write( Lf ), nl,

        % 12) le fumeur de gitane boit du vin
        lnelem( Lf, gitane, Pgit ),
        lnelem( Lb, vin, Pgit ),        %write( 'Contrainte 12 satisfaite: '), write(
Lf ), nl,

%%%%
        lperm( A,  La),
        % 8) L'espagnol a un chien              
        lnelem( Ln, espagnol, Pespagne ),
        lnelem( La, chien, Pespagne ),%write( 'Contrainte 8 satisfaite: '),
write( La ), nl,

        % 11) Le fumeur de old
...

read more »



Sat, 09 Feb 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Take five minutes to advertise Prolog

2. ASM/370 in five sessions...

3. Five Guys Cobol, Inc.

4. How to join five rows in one row?

5. "Smalltalk-Contracts (five positions)-IL"

6. Five Pound of Sugar in a One Pound Sack ;-)

7. Five bugs in CW2003 final version

8. FIVE WIN

9. Five Win Beginer

10. FrameText rated five stars at ClarionShop...

11. Five Minutes of your time is all I need

12. Five Win NG

 

 
Powered by phpBB® Forum Software