3.7.264. Zebra puzzle

A constraint that can be used for modelling the zebra puzzle problem. Here is the first known publication of that puzzle quoted in italic from Life International, December 17, 1962:

1. There are five houses.

2. The Englishman lives in the red house.

3. The Spaniard owns the dog.

4. Coffee is drunk in the green house.

5. The Ukrainian drinks tea.

6. The green house is immediately to the right of the ivory house.

7. The Old Gold smoker owns snails.

8. Kools are smoked in the yellow house.

9. Milk is drunk in the middle house.

10. The Norwegian lives in the first house.

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

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

13. The Lucky Strike smoker drinks orange juice.

14. The Japanese smokes Parliaments.

15. The Norwegian lives next to the blue house.

Now, who drinks water? Who owns the zebra?

In the interest of clarity, it must be added that each of the five houses is painted a different color, and their inhabitants are of different national extractions, own different pets, drink different beverages and smoke different brands of American cigarettes. In statement 6, right refers to the reader's right.

A first model involves $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$ constraints with variables in their tables (i.e.,Β the table of an $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$ constraint corresponds to its second argument). It consists of creating for each house $i$ ($1\beta €i\beta €5$) five variables ${C}_{i}$, ${N}_{i}$, ${A}_{i}$, ${D}_{i}$, ${B}_{i}$ respectively corresponding to the colour of house $i$, the nationality of the person leaving in house $i$, the preferred pet of the person leaving in house $i$, the preferred beverage of the person leaving in house $i$, the preferred brand of American cigarettes of the person leaving in house $i$. We first state the following five $\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi \pi \pi \pi }$ constraints on these variables for expressing the fact that colours, nationalities, pets, beverages, and brands of American cigarettes are distinct:

Now observe that most statements link two specific attributes (e.g.,Β The Englishman lives in the red house). Consequently, in order to ease the encoding of such statements in term of constraints, we will first create for each attribute a variable that indicates the house where an attribute occurs. For instance, for the statement The Englishman lives in the red house we will create two variables which respectively indicate in which house the Englishman lives and which house is red. We now create all the variables attached to each class of attributes.

For each possible colour $c\beta \left\{\mathrm{\pi \pi \pi },\mathrm{\pi \pi \pi \pi \pi },\mathrm{\pi \pi £\pi \pi \pi ¦},\mathrm{\pi ¦\pi \pi \pi \pi \pi €},\mathrm{\pi \pi \pi ’\pi }\right\}$ we create a variable ${I}_{c}$ that corresponds to the index of the house having this colour. For each variable ${I}_{c}$, an $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$ constraint links it to the variables ${C}_{1},{C}_{2},{C}_{3},{C}_{4},{C}_{5}$ giving the colour of each house:

Note that we can replace the five previous $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$ constraints by the following $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$ constraint:

• $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$$\left(β©\begin{array}{ccc}\mathrm{\pi \pi \pi \pi \pi ‘}-1\hfill & \mathrm{\pi \pi \pi \pi }-{C}_{1}\hfill & \mathrm{\pi \pi \pi \pi }-{I}_{\mathrm{\pi \pi \pi }},\hfill \\ \mathrm{\pi \pi \pi \pi \pi ‘}-2\hfill & \mathrm{\pi \pi \pi \pi }-{C}_{2}\hfill & \mathrm{\pi \pi \pi \pi }-{I}_{\mathrm{\pi \pi \pi \pi \pi }},\hfill \\ \mathrm{\pi \pi \pi \pi \pi ‘}-3\hfill & \mathrm{\pi \pi \pi \pi }-{C}_{3}\hfill & \mathrm{\pi \pi \pi \pi }-{I}_{\mathrm{\pi \pi £\pi \pi \pi ¦}},\hfill \\ \mathrm{\pi \pi \pi \pi \pi ‘}-4\hfill & \mathrm{\pi \pi \pi \pi }-{C}_{4}\hfill & \mathrm{\pi \pi \pi \pi }-{I}_{\mathrm{\pi ¦\pi \pi \pi \pi \pi €}},\hfill \\ \mathrm{\pi \pi \pi \pi \pi ‘}-5\hfill & \mathrm{\pi \pi \pi \pi }-{C}_{5}\hfill & \mathrm{\pi \pi \pi \pi }-{I}_{\mathrm{\pi \pi \pi ’\pi }}\hfill \end{array}βͺ\right)$

For each possible nationality we create a variable ${I}_{n}$ that corresponds to the index of the house where the person with this nationality lives. For each variable ${I}_{n}$, an $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$ constraint links it to the variables ${N}_{1},{N}_{2},{N}_{3},{N}_{4},{N}_{5}$ giving the nationality associated with each house:

• , $\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi }=2$, $\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi }=3$, $\mathrm{\pi \pi \pi \pi €\pi \pi \pi \pi \pi }=4$, ,

• $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$$\left({I}_{\mathrm{\pi ’\pi \pi \pi \pi \pi \pi \pi \pi }},\beta ©{N}_{1},{N}_{2},{N}_{3},{N}_{4},{N}_{5}\beta ͺ,\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi }\right)$,

• $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$$\left({I}_{\mathrm{\pi \pi \pi \pi €\pi \pi \pi \pi \pi }},\beta ©{N}_{1},{N}_{2},{N}_{3},{N}_{4},{N}_{5}\beta ͺ,\mathrm{\pi \pi \pi \pi €\pi \pi \pi \pi \pi }\right)$,

Again we can replace the five previous $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$ constraints by the following $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$ constraint:

For each possible preferred pet we create a variable ${I}_{a}$ that corresponds to the index of the house where the person that prefers this pet lives. For each variable ${I}_{a}$, an $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$ constraint links it to the variables ${A}_{1},{A}_{2},{A}_{3},{A}_{4},{A}_{5}$ giving the preferred pet of each house:

Again we can replace the five previous $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$ constraints by the following $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$ constraint:

For each possible preferred beverage $d\beta \left\{\mathrm{\pi \pi \pi \pi \pi \pi },\mathrm{\pi ‘\pi \pi },\mathrm{\pi \pi \pi \pi },\mathrm{\pi \pi \pi \pi \pi \pi }_\mathrm{\pi \pi ’\pi \pi \pi },\mathrm{\pi €\pi \pi ‘\pi \pi }\right\}$ we create a variable ${I}_{d}$ that corresponds to the index of the house where the person that prefers this beverage lives. For each variable ${I}_{d}$, an $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$ constraint links it to the variables ${D}_{1},{D}_{2},{D}_{3},{D}_{4},{D}_{5}$ giving the preferred beverage of each house:

Again we can replace the five previous $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$ constraints by the following $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$ constraint:

• $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$$\left(β©\begin{array}{ccc}\mathrm{\pi \pi \pi \pi \pi ‘}-1\hfill & \mathrm{\pi \pi \pi \pi }-{D}_{1}\hfill & \mathrm{\pi \pi \pi \pi }-{I}_{\mathrm{\pi \pi \pi \pi \pi \pi }},\hfill \\ \mathrm{\pi \pi \pi \pi \pi ‘}-2\hfill & \mathrm{\pi \pi \pi \pi }-{D}_{2}\hfill & \mathrm{\pi \pi \pi \pi }-{I}_{\mathrm{\pi ‘\pi \pi }},\hfill \\ \mathrm{\pi \pi \pi \pi \pi ‘}-3\hfill & \mathrm{\pi \pi \pi \pi }-{D}_{3}\hfill & \mathrm{\pi \pi \pi \pi }-{I}_{\mathrm{\pi \pi \pi \pi }},\hfill \\ \mathrm{\pi \pi \pi \pi \pi ‘}-4\hfill & \mathrm{\pi \pi \pi \pi }-{D}_{4}\hfill & \mathrm{\pi \pi \pi \pi }-{I}_{\mathrm{\pi \pi \pi \pi \pi \pi }_\mathrm{\pi \pi ’\pi \pi \pi }},\hfill \\ \mathrm{\pi \pi \pi \pi \pi ‘}-5\hfill & \mathrm{\pi \pi \pi \pi }-{D}_{5}\hfill & \mathrm{\pi \pi \pi \pi }-{I}_{\mathrm{\pi €\pi \pi ‘\pi \pi }}\hfill \end{array}βͺ\right)$

For each possible preferred brand of American cigarettes $b\beta \left\{\mathrm{\pi \pi \pi }_\mathrm{\pi \pi \pi \pi },\mathrm{\pi \pi \pi \pi },$ we create a variable ${I}_{b}$ that corresponds to the index of the house where the person that prefers this brand lives. For each variable ${I}_{b}$, an $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$ constraint links it to the variables ${B}_{1},{B}_{2},{B}_{3},{B}_{4},{B}_{5}$ giving the preferred brand of American cigarettes of each house:

Again we can replace the five previous $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$ constraints by the following $\mathrm{\pi \pi \pi \pi \pi \pi \pi }$ constraint:

Finally we state one constraint for each statement from 2 to 15:

• Β (the Englishman lives in the red house).

• Β (the Spaniard owns the dog).

• ${I}_{\mathrm{\pi \pi \pi \pi \pi \pi }}={I}_{\mathrm{\pi \pi \pi \pi \pi }}$Β (coffee is drunk in the green house).

• ${I}_{\mathrm{\pi ’\pi \pi \pi \pi \pi \pi \pi \pi }}={I}_{\mathrm{\pi ‘\pi \pi }}$Β (the Ukrainian drinks tea).

• ${I}_{\mathrm{\pi \pi \pi \pi \pi }}={I}_{\mathrm{\pi \pi £\pi \pi \pi ¦}}+1$Β (the green house is immediately to the right of the ivory house).

• Β (the Old Gold smoker owns snails).

• ${I}_{\mathrm{\pi \pi \pi \pi }}={I}_{\mathrm{\pi ¦\pi \pi \pi \pi \pi €}}$Β (kools are smoked in the yellow house).

• ${I}_{\mathrm{\pi \pi \pi \pi }}=3$Β (milk is drunk in the middle house).

• ${I}_{\mathrm{\pi \pi \pi \pi €\pi \pi \pi \pi \pi }}=1$Β (the Norwegian lives in the first house).

• Β (the man who smokes Chesterfields lives in the house next to the man with the fox).

• Β (kools are smoked in the house next to the house where the horse is kept).

• Β (the Lucky Strike smoker drinks orange juice).

• Β (the Japanese smokes Parliaments).

• $|{I}_{\mathrm{\pi \pi \pi \pi €\pi \pi \pi \pi \pi }}-{I}_{\mathrm{\pi \pi \pi ’\pi }}|=1$Β (the Norwegian lives next to the blue house).

Now note that variables ${C}_{i}$, ${N}_{i}$, ${A}_{i}$, ${D}_{i}$, ${B}_{i}$ ($1\beta €i\beta €5$) do not occur at all within the constraints encoding statements 2 to 15. Consequently they can be removed, as long as we replace the five $\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi \pi \pi \pi }$ constraints on these variables by the following $\mathrm{\pi \pi \pi \pi \pi \pi \pi \pi \pi \pi \pi \pi }$ constraints:

In our experience, when confronted for the first time to this puzzle, a lot of people come up with the model that associates to each house $i$ ($1\beta €i\beta €5$) five variables ${C}_{i}$, ${N}_{i}$, ${A}_{i}$, ${D}_{i}$, ${B}_{i}$ that describe the attributes of the person living in house $i$. However it is difficult to directly express the constraints according to these variables and the second model which associates to each attribute a variable that gives the corresponding house is more convenient for expressing the constraints.