% A problem posed by Daniel Ligett (eagle!harpo!decvax!wivax!ligett)
 
% (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 
% 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 Ukrainian 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.

% THE PROBLEM: Who owns the Zebra?  Who drinks water?
% Dan Ligett   decvax!wivax!ligett   Wang Institute, Tyngsboro MA 01879



% This is not the most efficient way of doing it, but it is
% very straightforward. The trick to most of these puzzles is
% choosing the right representation.

% Execute the program by "puzzle!".
%						Claude.

% Each house is represented by a structure of the form:
%	house(Colour, Nationality, Pet, Drink, Cigarette)


puzzle :-
	houses(Houses),

%  The Englishman lives in the red house.

	member(house(red, english, _, _, _), Houses),

%  The Spaniard owns the dog.

	member(house(_, spanish, dog, _, _), Houses),

%  Coffee is drunk in the green house.

	member(house(green, _, _, coffee, _), Houses),

%  The Ukrainian drinks tea.

	member(house(_, ukranian, _, tea, _), Houses),

%  The green house is immediately to the right (your right) of the
%  ivory house.

	right_of(house(green,_,_,_,_), house(ivory,_,_,_,_), Houses),

%  The Winston smoker owns snails.

	member(house(_, _, snails, _, winstons), Houses),

%  Kools are smoked in the yellow house.

	member(house(yellow, _, _, _, kools), Houses),

%  Milk is drunk in the middle house.

	Houses = [_, _, house(_, _, _, milk, _), _,_],

%  The Norwegian lives in the first house on the left.

	Houses = [house(_, norwegian, _, _, _)|_],

% The man who smokes Chesterfields lives in the house next to the
% man with the fox.

	next_to(house(_,_,_,_,chesterfields), house(_,_,fox,_,_), Houses),

% Kools are smoked in the house next to the house where the horse
% is kept.

	next_to(house(_,_,_,_,kools), house(_,_,horse,_,_), Houses),

% The Lucky Strike smoker drinks orange juice.

	member(house(_, _, _, orange_juice, lucy_strikes), Houses),

% The Japanese smokes Parliaments.

	member(house(_, japanese, _, _, parliaments), Houses),

% The Norwegian lives next to the blue house.

	next_to(house(_,norwegian,_,_,_), house(blue,_,_,_,_), Houses),

% The house which contains the zebra.

	member(house(_, _, zebra, _, _), Houses),

% The house which has the water.

	member(house(_, _, _, water, _), Houses),

% Print result

	print_houses(Houses).

houses([
	house(_, _, _, _, _),
	house(_, _, _, _, _),
	house(_, _, _, _, _),
	house(_, _, _, _, _),
	house(_, _, _, _, _)
]).

right_of(A, B, [B, A | _]).
right_of(A, B, [X | Y]) :- right_of(A, B, Y).

next_to(A, B, [A, B | _]).
next_to(A, B, [B, A | _]).
next_to(A, B, [X | Y]) :- next_to(A, B, Y).

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

print_houses([A|B]) :- !,
	print(A),
	print_houses(B).
print_houses([]).
