## Sunday, January 13, 2013

### Einstein's Fish

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. Where ever 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?

This is a simple constraint satisfaction problem.  Let us start by defining a regular language that consists of all the possible solutions for the puzzle. We need five basic variables: Color of the house, Nationality of the owner, and his favorite Drink, Cigarette, and Pet. We define each variable as the language of the possible values of the variable.
define Color [blue | green | red | white | yellow];
define Nationality [Dane | Englishman | German | Swede | Norwegian];
define Drink [bier | coffee | milk |tea | water];
define Cigarette [Blend | BlueMaster | Dunhill | PallMall | Prince];
define Pet [birds | cats | dogs | fish | horses];

The next concept to define is that of a House. Let us construe it as a concatenation of the five terms defined above:
define     House     [Color Nationality Drink Cigarette Pet];
With five variables each taking one of five possible values, this gives quite a number of possible households, 5x5x5x5x5 = 3125, to be exact. A road with five houses next to each other, House^5, provides an astronomical number of possible combinations of colors, nationalities, drinks, cigarettes and pets.
To solve Einstein’s puzzle, we represent each of the fifteen constraints as a regular language and intersect these languages with the initial set of all possibilities. If all goes well, at the end we will know who keeps fish. For example, we can interpret The Englishman lives in the red house. as \$[red Englishman]. This constraint is trivial to encode because in our representation of a house, the color and the nationality are adjacent. The second costraint, The Swede keeps dogs could be represented as \$[Swede Drink Cigarette dogs] but we will choose a less verbose formulation, \$[Swede ~\$Pet dogs], that does not explicitly list the variables that separate Nationality and Pet. The fifteen constraints are shown below.
define     C1     \$[red Englishman];
# The Englishman lives in the red house.
define     C2     \$[Swede ~\$Pet dogs];
# The Swede keeps dogs.
define     C3     \$[Dane tea];
# The Dane drinks tea.
define     C4     \$[green ~\$Color white];
# The green house is just to the left of the white one.
define     C5     \$[green ~\$Drink coffee];
# The owner of the green house drinks coffee.
define     C6     \$[PallMall birds];
# The Pall Mall smoker keeps birds.
define     C7     \$[yellow ~\$Cigarette Dunhill];
# The owner of the yellow house smokes Dunhills.
define     C8     [House^2 ~\$Drink milk ~\$Drink House^2];
# The man in the center house drinks milk.
define     C9     [? Norwegian ?*];
# The Norwegian lives in the first house.
define     C10     \$[Blend ? ~\$Pet cats | cats ~\$Cigarette Blend];
# The Blend smoker has a neighbor who keeps cats.
define     C11     \$[horses ~\$Cigarette Dunhill | Dunhill ? ~\$Pet horses];
# The man who keeps horses lives next to the Dunhill smoker.
define     C12     \$[bier BlueMaster];
# The man who smokes Blue Masters drinks bier.
define     C13     \$[German ~\$Cigarette Prince];
# The German smokes Prince.
define     C14     \$[Norwegian ~\$Color blue | blue ? ~\$Nationality Norwegian];
# The Norwegian lives next to the blue house.
define     C15     \$[Blend ~\$Drink water | water ? ~\$Cigarette Blend];
# The Blend smoker has a neighbor who drinks water.
All that we need to do to solve the problem is to impose the constraints on the row of five houses by intersection. The solution below is almost correct.
define     Solution [House^5 & C1 & C2 & C3 &C4 & C5 &
C6 & C7 & C8 & C9 & C10 &
C11 & C12 & C13 & C14 & C15];
The result is a network with five paths. In four of the solutions nobody keeps fish and the German keeps the same kind of pet as someone else. We need one final constraint, presupposed by the question Who keeps fish?:
define     C16     \$fish;
# There is someone who keeps fish.
With C16 added, only one solution remains. To make it easier to see, we compose the solution with the transducer that adds some prose around the pieces along the one remaining path:
define     Describe     [House -> "In the " ... .o.
Color -> ... " house " .o.
Nationality -> "the " ... " " .o.
Drink -> "drinks "... ", " .o.
Cigarette -> "smokes "... "s,\n and " .o.
Pet -> "keeps " ... ".\n"] ;
We can now see the solution!

regex [Solution .o. Describe];
76 states, 75 arcs, 1 path.
xfst[2]: print lower-words
In the yellow house the Norwegian drinks water, smokes Dunhills,
and keeps cats.
In the blue house the Dane drinks tea, smokes Blends,
and keeps horses.
In the red house the Englishman drinks milk, smokes PallMalls,
and keeps birds.
In the green house the German drinks coffee, smokes Princes,
and keeps fish.
In the white house the Swede drinks bier, smokes BlueMasters,
and keeps dogs.
In short, it’s the German who keeps fish.