Zebra puzzle: Difference between revisions

From Rosetta Code
Content added Content deleted
(start it up with a Prolog section, as a draft. If it deserves to be a task, please promote it. I think it does, it is a widely known puzzle.)
 
m (play it safe before anyone raises questions)
Line 13: Line 13:
#The Norwegian lives in the first house.
#The Norwegian lives in the first house.
#The man who smokes Blend lives in the house next to the house with cats.
#The man who smokes Blend lives in the house next to the house with cats.
#In the house next to the house where they have a horse, they smoke Dunhill.
#In a house next to the house where they have a horse, they smoke Dunhill.
#The man who smokes Blue Master drinks beer.
#The man who smokes Blue Master drinks beer.
#The German smokes Prince.
#The German smokes Prince.
#The Norwegian lives next to the blue house.
#The Norwegian lives next to the blue house.
#They drink water in the house next to the house where they smoke Blend.
#They drink water in a house next to the house where they smoke Blend.


The question is, who ownes the zebra?
The question is, who ownes the zebra?

Revision as of 18:03, 2 December 2011

Zebra puzzle is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The Zebra puzzle, a.k.a. Einstein's Riddle, is a logic puzzle which is to be solved programmatically. It has several variants, one of them this:

  1. There are five houses.
  2. The English man lives in the red house.
  3. The Swede has a dog.
  4. The Dane drinks tea.
  5. The green house is immediately to the left of the white house.
  6. They drink coffee in the green house.
  7. The man who smokes Pall Mall has birds.
  8. In the yellow house they smoke Dunhill.
  9. In the middle house they drink milk.
  10. The Norwegian lives in the first house.
  11. The man who smokes Blend lives in the house next to the house with cats.
  12. In a house next to the house where they have a horse, they smoke Dunhill.
  13. The man who smokes Blue Master drinks beer.
  14. The German smokes Prince.
  15. The Norwegian lives next to the blue house.
  16. They drink water in a house next to the house where they smoke Blend.

The question is, who ownes the zebra?

Prolog

In Prolog we can specify the domain by selecting from it, making mutually exclusive choices for efficiency:

<lang Prolog>select([A|As],S):- select(A,S,S1),select(As,S1). select([],_).

next_to(A,B,C):- left_of(A,B,C),left_of(B,A,C). left_of(A,B,C):- append(_,[A,B|_],C).

zebra(Owns, HS):-  % color,nation,pet,drink,smokes

     HS = [h(_,norwegian,_,_,_),h(blue,_,_,_,_),h(_,_,_,milk,_),_,_], 
     select( [h(red,eng,_,_,_),h(_,swede,dog,_,_),h(_,dane,_,tea,_),
              h(_,german,_,_,prince)], HS),
     select( [h(_,_,birds,_,pallmall),h(yellow,_,_,_,dunhill),
              h(_,_,_,beer,bluemaster)], HS), 
     left_of( h(green,_,_,coffee,_),h(white,_,_,_,_), HS),
     next_to( h(_,_,_,_,dunhill),h(_,_,horse,_,_), HS),
     next_to( h(_,_,_,_,blend),  h(_,_,cats, _,_), HS),
     next_to( h(_,_,_,_,blend)  ,h(_,_,_,water,_), HS),
     member(  h(_,Owns,zebra,_,_), HS).
- findall(Who,zebra(Who,_),L).</lang>

Output: <lang Prolog> L = [german].</lang>

Works with SWI-Prolog.