House Puzzle

From the data provided below, determine who drinks water and who owns the zebra.

There are five houses each of a different color and inhabited by men of different nationalities, with different pets, drinks, and brands of cigarettes.

  1. The Englishman lives in the red house.
  2. The Spaniard owns the dog.
  3. Coffee is drunk in the green house.
  4. The Ukranian drinks tea.
  5. The green house is immediately to the right (your right) of the ivory house.
  6. The Winston smoker owns snails.
  7. Kools are smoked in the yellow house.
  8. Milk is drunk in the middle house.
  9. The Norwegian lives in the first house on the left.
  10. The man who smokes Chesterfields lives in the house next to the man with the fox.
  11. Kools are smoked in the house next to the house where the horse is kept.
  12. The Lucky Strike smoker drinks orange juice.
  13. The Japanese smokes Parliaments.
  14. The Norwegian lives next to the blue house.
check_determ
domains
  colour      = symbol
  nationality = symbol
  pet         = symbol
  drink       = symbol
  smoke       = symbol
  house       = reference house(colour,nationality,pet,drink,smoke)
  houses      = houses(house,house,house,house,house)
predicates
  nondeterm solve
  nondeterm clues(houses)
  nondeterm queries(houses)
  colour(house,colour)
  nationality(house,nationality)
  pet(house,pet)
  drink(house,drink)
  smoke(house,smoke)
  first(houses,house)
  middle(houses,house)  
  nondeterm immed_to_right(houses,house,house)
  nondeterm next_to(houses,house,house)
  nondeterm member(house,houses)
clauses

solve:-
  clues(Houses),
  queries(Houses).

clues(Houses):-
  member(A, Houses), colour(A, red), nationality(A, english),        /*  1 */
  member(B, Houses), nationality(B, spaniard), pet(B, dog),          /*  2 */
  member(C, Houses), colour(C, green), drink(C, coffee),             /*  3 */
  member(D, Houses), nationality(D, ukranian), drink(D, tea),        /*  4 */
  immed_to_right(Houses, E, F), colour(E, green), colour(F, ivory),  /*  5 */
  member(G, Houses), smoke(G, winston), pet(G, snails),              /*  6 */
  member(H, Houses), smoke(H, kools), colour(H, yellow),             /*  7 */
  middle(Houses, I), drink(I, milk),                                 /*  8 */
  first(Houses, J), nationality(J, norwegian),                       /*  9 */
  next_to(Houses, K, L), smoke(K, chesterfields), pet(L, fox),       /* 10 */
  next_to(Houses, M, N), smoke(M, kools), pet(N, horse),             /* 11 */
  member(O, Houses), smoke(O, luckystrike), drink(O, orangejuice),   /* 12 */
  member(P, Houses), nationality(P, japanese), smoke(P, parliaments),/* 13 */
  next_to(Houses, Q, R), nationality(Q, norwegian), colour(R, blue). /* 14 */

queries(Houses):-
  member(X, Houses), pet(X, zebra), nationality(X, Nationality1),
  writef("The % owns the zebra\n", Nationality1), /* japanese  */
  member(Y, Houses), drink(Y, water), nationality(Y, Nationality2),
  writef("The % drinks water\n", Nationality2).   /* norwegian */
  
colour(house(C,_,_,_,_), C). 
nationality(house(_,N,_,_,_), N). 
pet(house(_,_,P,_,_), P). 
drink(house(_,_,_,D,_), D). 
smoke(house(_,_,_,_,S), S). 

first(houses(X,_,_,_,_), X).  

middle(houses(_,_,X,_,_), X).  

immed_to_right(houses(L,R,_,_,_), R, L).
immed_to_right(houses(_,L,R,_,_), R, L).
immed_to_right(houses(_,_,L,R,_), R, L).
immed_to_right(houses(_,_,_,L,R), R, L).

next_to(Xs, X, Y):-
  immed_to_right(Xs, X, Y).
next_to(Xs, X, Y):-
  immed_to_right(Xs, Y, X).

member(X, houses(X,_,_,_,_)).
member(X, houses(_,X,_,_,_)).
member(X, houses(_,_,X,_,_)).
member(X, houses(_,_,_,X,_)).
member(X, houses(_,_,_,_,X)).

Turbo Prolog Index     Home Page