Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
solve Einstein's Puzzle using ECLiPSe Prolog
% Constraints satisfaction problem from Albert Einstein's Puzzle
% Author : Feng Di @K.U.Leuven
% Please run this program by ECLiPSe
% ECLiPSe is a constraint processing tool using Prolog
:-lib(ic).
einstein_problem:-
% I use 5 list of 5 variables
Nat = [English,Swedes,Danish,Norwegian,German],
Color = [Red,White,Green,Yellow,Blue],
Pat = [Dog,Bird,Cat,Horse,Fish],
Drink = [Tea,Coffee,Milk,Beer,Water],
Cigarette = [PallMall,Dunhill,Blends,BlueMaster,Prince],
%domain of variables
Nat :: 1..5,
Color :: 1..5,
Pat :: 1..5,
Drink :: 1..5,
Cigarette :: 1..5,
%constraints
alldifferent(Nat),
alldifferent(Color),
alldifferent(Pat),
alldifferent(Drink),
alldifferent(Cigarette),
English $= Red,
Swedes $= Dog,
Danish $= Tea,
White #= Green+1,
Green $= Coffee,
PallMall $= Bird,
Yellow $= Dunhill,
Milk $= 3,
Norwegian $= 1,
abs(Blends-Cat) $= 1,
abs(Horse-Dunhill) $= 1,
BlueMaster $= Beer,
German $= Prince,
abs(Norwegian-Blue) $= 1,
abs(Blends-Water) $=1,
%search
flatten([Nat,Color,Pat,Drink,Cigarette],List),
labeling(List),
%print solution
NatNames = [English-english,Swedes-swedes,Danish-danish,Norwegian-norwegian,German-german],
memberchk(Fish-FishNat,NatNames),
write('The '),write(FishNat),write(' owns fish.'),nl.
Owner

JuanitoFatas commented Mar 28, 2012

Einstein's Puzzle
Variations of this riddle appear on the net from time to time. It is sometimes attributed to Albert Einstein and it is claimed that 98% of the people are incapable of solving it. Some commentators suggest that Einstein created such puzzles not to test out intelligence but to get rid of all the students who wanted him as an advisor. It is not likely that there is any truth to these stories. Whereever this comes from, it is a nice riddle.

Let us assume that there are five houses of different colors next to each other on the same road. In each house lives a man of a different nationality. Every man has his favorite drink, his favorite brand of cigarettes, and keeps pets of a particular kind.
The Englishman lives in the red house.
The Swede keeps dogs.
The Dane drinks tea.
The green house is just to the left of the white one.
The owner of the green house drinks coffee.
The Pall Mall smoker keeps birds.
The owner of the yellow house smokes Dunhills.
The man in the center house drinks milk.
The Norwegian lives in the first house.
The Blend smoker has a neighbor who keeps cats.
The man who smokes Blue Masters drinks bier.
The man who keeps horses lives next to the Dunhill smoker.
The German smokes Prince.
The Norwegian lives next to the blue house.
The Blend smoker has a neighbor who drinks water.
The question to be answered is: Who keeps fish?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment