### 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{𝚎𝚕𝚎𝚖𝚎𝚗𝚝}$ constraints with variables in their tables (i.e., the table of an $\mathrm{𝚎𝚕𝚎𝚖𝚎𝚗𝚝}$ constraint corresponds to its second argument). It consists of creating for each house $i$ ($1\le i\le 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{𝚊𝚕𝚕𝚍𝚒𝚏𝚏𝚎𝚛𝚎𝚗𝚝}$ 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\in \left\{\mathrm{𝑟𝑒𝑑},\mathrm{𝑔𝑟𝑒𝑒𝑛},\mathrm{𝑖𝑣𝑜𝑟𝑦},\mathrm{𝑦𝑒𝑙𝑙𝑜𝑤},\mathrm{𝑏𝑙𝑢𝑒}\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{𝚎𝚕𝚎𝚖𝚎𝚗𝚝}$ 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{𝚎𝚕𝚎𝚖𝚎𝚗𝚝}$ constraints by the following $\mathrm{𝚒𝚗𝚟𝚎𝚛𝚜𝚎}$ constraint:

• $\mathrm{𝚒𝚗𝚟𝚎𝚛𝚜𝚎}$$\left(〈\begin{array}{ccc}\mathrm{𝚒𝚗𝚍𝚎𝚡}-1\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{C}_{1}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑟𝑒𝑑}},\hfill \\ \mathrm{𝚒𝚗𝚍𝚎𝚡}-2\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{C}_{2}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑔𝑟𝑒𝑒𝑛}},\hfill \\ \mathrm{𝚒𝚗𝚍𝚎𝚡}-3\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{C}_{3}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑖𝑣𝑜𝑟𝑦}},\hfill \\ \mathrm{𝚒𝚗𝚍𝚎𝚡}-4\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{C}_{4}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑦𝑒𝑙𝑙𝑜𝑤}},\hfill \\ \mathrm{𝚒𝚗𝚍𝚎𝚡}-5\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{C}_{5}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑏𝑙𝑢𝑒}}\hfill \end{array}〉\right)$

For each possible nationality $n\in \left\{\mathrm{𝑒𝑛𝑔𝑙𝑖𝑠ℎ𝑚𝑎𝑛},\mathrm{𝑠𝑝𝑎𝑛𝑖𝑎𝑟𝑑},\mathrm{𝑢𝑘𝑟𝑎𝑖𝑛𝑖𝑎𝑛},\mathrm{𝑛𝑜𝑟𝑤𝑒𝑔𝑖𝑎𝑛},$ $\mathrm{𝑗𝑎𝑝𝑎𝑛𝑒𝑠𝑒}\right\}$ 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{𝚎𝚕𝚎𝚖𝚎𝚗𝚝}$ constraint links it to the variables ${N}_{1},{N}_{2},{N}_{3},{N}_{4},{N}_{5}$ giving the nationality associated with each house:

Again we can replace the five previous $\mathrm{𝚎𝚕𝚎𝚖𝚎𝚗𝚝}$ constraints by the following $\mathrm{𝚒𝚗𝚟𝚎𝚛𝚜𝚎}$ constraint:

• $\mathrm{𝚒𝚗𝚟𝚎𝚛𝚜𝚎}$$\left(〈\begin{array}{ccc}\mathrm{𝚒𝚗𝚍𝚎𝚡}-1\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{N}_{1}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑒𝑛𝑔𝑙𝑖𝑠ℎ𝑚𝑎𝑛}},\hfill \\ \mathrm{𝚒𝚗𝚍𝚎𝚡}-2\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{N}_{2}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑠𝑝𝑎𝑛𝑖𝑎𝑟𝑑}},\hfill \\ \mathrm{𝚒𝚗𝚍𝚎𝚡}-3\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{N}_{3}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑢𝑘𝑟𝑎𝑖𝑛𝑖𝑎𝑛}},\hfill \\ \mathrm{𝚒𝚗𝚍𝚎𝚡}-4\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{N}_{4}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑛𝑜𝑟𝑤𝑒𝑔𝑖𝑎𝑛}},\hfill \\ \mathrm{𝚒𝚗𝚍𝚎𝚡}-5\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{N}_{5}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑗𝑎𝑝𝑎𝑛𝑒𝑠𝑒}}\hfill \end{array}〉\right)$

For each possible preferred pet $a\in \left\{\mathrm{𝑑𝑜𝑔},\mathrm{𝑠𝑛𝑎𝑖𝑙},\mathrm{𝑓𝑜𝑥},\mathrm{ℎ𝑜𝑟𝑠𝑒},\mathrm{𝑧𝑒𝑏𝑟𝑎}\right\}$ 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{𝚎𝚕𝚎𝚖𝚎𝚗𝚝}$ 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{𝚎𝚕𝚎𝚖𝚎𝚗𝚝}$ constraints by the following $\mathrm{𝚒𝚗𝚟𝚎𝚛𝚜𝚎}$ constraint:

• $\mathrm{𝚒𝚗𝚟𝚎𝚛𝚜𝚎}$$\left(〈\begin{array}{ccc}\mathrm{𝚒𝚗𝚍𝚎𝚡}-1\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{A}_{1}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑑𝑜𝑔}},\hfill \\ \mathrm{𝚒𝚗𝚍𝚎𝚡}-2\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{A}_{2}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑠𝑛𝑎𝑖𝑙}},\hfill \\ \mathrm{𝚒𝚗𝚍𝚎𝚡}-3\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{A}_{3}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑓𝑜𝑥}},\hfill \\ \mathrm{𝚒𝚗𝚍𝚎𝚡}-4\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{A}_{4}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{ℎ𝑜𝑟𝑠𝑒}},\hfill \\ \mathrm{𝚒𝚗𝚍𝚎𝚡}-5\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{A}_{5}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑧𝑒𝑏𝑟𝑎}}\hfill \end{array}〉\right)$

For each possible preferred beverage $d\in \left\{\mathrm{𝑐𝑜𝑓𝑓𝑒𝑒},\mathrm{𝑡𝑒𝑎},\mathrm{𝑚𝑖𝑙𝑘},\mathrm{𝑜𝑟𝑎𝑛𝑔𝑒}_\mathrm{𝑗𝑢𝑖𝑐𝑒},\mathrm{𝑤𝑎𝑡𝑒𝑟}\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{𝚎𝚕𝚎𝚖𝚎𝚗𝚝}$ 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{𝚎𝚕𝚎𝚖𝚎𝚗𝚝}$ constraints by the following $\mathrm{𝚒𝚗𝚟𝚎𝚛𝚜𝚎}$ constraint:

• $\mathrm{𝚒𝚗𝚟𝚎𝚛𝚜𝚎}$$\left(〈\begin{array}{ccc}\mathrm{𝚒𝚗𝚍𝚎𝚡}-1\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{D}_{1}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑐𝑜𝑓𝑓𝑒𝑒}},\hfill \\ \mathrm{𝚒𝚗𝚍𝚎𝚡}-2\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{D}_{2}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑡𝑒𝑎}},\hfill \\ \mathrm{𝚒𝚗𝚍𝚎𝚡}-3\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{D}_{3}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑚𝑖𝑙𝑘}},\hfill \\ \mathrm{𝚒𝚗𝚍𝚎𝚡}-4\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{D}_{4}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑜𝑟𝑎𝑛𝑔𝑒}_\mathrm{𝑗𝑢𝑖𝑐𝑒}},\hfill \\ \mathrm{𝚒𝚗𝚍𝚎𝚡}-5\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{D}_{5}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑤𝑎𝑡𝑒𝑟}}\hfill \end{array}〉\right)$

For each possible preferred brand of American cigarettes $b\in \left\{\mathrm{𝑜𝑙𝑑}_\mathrm{𝑔𝑜𝑙𝑑},\mathrm{𝑘𝑜𝑜𝑙},$ $\mathrm{𝑐ℎ𝑒𝑠𝑡𝑒𝑟𝑓𝑖𝑒𝑙𝑑},\mathrm{𝑙𝑢𝑐𝑘𝑦}_\mathrm{𝑠𝑡𝑟𝑖𝑘𝑒},\mathrm{𝑝𝑎𝑟𝑙𝑖𝑎𝑚𝑒𝑛𝑡}\right\}$ 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{𝚎𝚕𝚎𝚖𝚎𝚗𝚝}$ 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{𝚎𝚕𝚎𝚖𝚎𝚗𝚝}$ constraints by the following $\mathrm{𝚒𝚗𝚟𝚎𝚛𝚜𝚎}$ constraint:

• $\mathrm{𝚒𝚗𝚟𝚎𝚛𝚜𝚎}$$\left(〈\begin{array}{ccc}\mathrm{𝚒𝚗𝚍𝚎𝚡}-1\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{B}_{1}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑜𝑙𝑑}_\mathrm{𝑔𝑜𝑙𝑑}},\hfill \\ \mathrm{𝚒𝚗𝚍𝚎𝚡}-2\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{B}_{2}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑘𝑜𝑜𝑙}},\hfill \\ \mathrm{𝚒𝚗𝚍𝚎𝚡}-3\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{B}_{3}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑐ℎ𝑒𝑠𝑡𝑒𝑟𝑓𝑖𝑒𝑙𝑑}},\hfill \\ \mathrm{𝚒𝚗𝚍𝚎𝚡}-4\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{B}_{4}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑙𝑢𝑐𝑘𝑦}_\mathrm{𝑠𝑡𝑟𝑖𝑘𝑒}},\hfill \\ \mathrm{𝚒𝚗𝚍𝚎𝚡}-5\hfill & \mathrm{𝚜𝚞𝚌𝚌}-{B}_{5}\hfill & \mathrm{𝚙𝚛𝚎𝚍}-{I}_{\mathrm{𝑝𝑎𝑟𝑙𝑖𝑎𝑚𝑒𝑛𝑡}}\hfill \end{array}〉\right)$

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

• ${I}_{\mathrm{𝑒𝑛𝑔𝑙𝑖𝑠ℎ𝑚𝑎𝑛}}={I}_{\mathrm{𝑟𝑒𝑑}}$ (the Englishman lives in the red house).

• ${I}_{\mathrm{𝑠𝑝𝑎𝑛𝑖𝑎𝑟𝑑}}={I}_{\mathrm{𝑑𝑜𝑔}}$ (the Spaniard owns the dog).

• ${I}_{\mathrm{𝑐𝑜𝑓𝑓𝑒𝑒}}={I}_{\mathrm{𝑔𝑟𝑒𝑒𝑛}}$ (coffee is drunk in the green house).

• ${I}_{\mathrm{𝑢𝑘𝑟𝑎𝑖𝑛𝑖𝑎𝑛}}={I}_{\mathrm{𝑡𝑒𝑎}}$ (the Ukrainian drinks tea).

• ${I}_{\mathrm{𝑔𝑟𝑒𝑒𝑛}}={I}_{\mathrm{𝑖𝑣𝑜𝑟𝑦}}+1$ (the green house is immediately to the right of the ivory house).

• ${I}_{\mathrm{𝑜𝑙𝑑}_\mathrm{𝑔𝑜𝑙𝑑}}={I}_{\mathrm{𝑠𝑛𝑎𝑖𝑙}}$ (the Old Gold smoker owns snails).

• ${I}_{\mathrm{𝑘𝑜𝑜𝑙}}={I}_{\mathrm{𝑦𝑒𝑙𝑙𝑜𝑤}}$ (kools are smoked in the yellow house).

• ${I}_{\mathrm{𝑚𝑖𝑙𝑘}}=3$ (milk is drunk in the middle house).

• ${I}_{\mathrm{𝑛𝑜𝑟𝑤𝑒𝑔𝑖𝑎𝑛}}=1$ (the Norwegian lives in the first house).

• $|{I}_{\mathrm{𝑐ℎ𝑒𝑠𝑡𝑒𝑟𝑓𝑖𝑒𝑙𝑑}}-{I}_{\mathrm{𝑓𝑜𝑥}}|=1$ (the man who smokes Chesterfields lives in the house next to the man with the fox).

• $|{I}_{\mathrm{𝑘𝑜𝑜𝑙}}-{I}_{\mathrm{ℎ𝑜𝑟𝑠𝑒}}|=1$ (kools are smoked in the house next to the house where the horse is kept).

• ${I}_{\mathrm{𝑙𝑢𝑐𝑘𝑦}_\mathrm{𝑠𝑡𝑟𝑖𝑘𝑒}}={I}_{\mathrm{𝑜𝑟𝑎𝑛𝑔𝑒}_\mathrm{𝑗𝑢𝑖𝑐𝑒}}$ (the Lucky Strike smoker drinks orange juice).

• ${I}_{\mathrm{𝑗𝑎𝑝𝑎𝑛𝑒𝑠𝑒}}={I}_{\mathrm{𝑝𝑎𝑟𝑙𝑖𝑎𝑚𝑒𝑛𝑡}}$ (the Japanese smokes Parliaments).

• $|{I}_{\mathrm{𝑛𝑜𝑟𝑤𝑒𝑔𝑖𝑎𝑛}}-{I}_{\mathrm{𝑏𝑙𝑢𝑒}}|=1$ (the Norwegian lives next to the blue house).

Now note that variables ${C}_{i}$, ${N}_{i}$, ${A}_{i}$, ${D}_{i}$, ${B}_{i}$ ($1\le i\le 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{𝚊𝚕𝚕𝚍𝚒𝚏𝚏𝚎𝚛𝚎𝚗𝚝}$ constraints on these variables by the following $\mathrm{𝚊𝚕𝚕𝚍𝚒𝚏𝚏𝚎𝚛𝚎𝚗𝚝}$ 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\le i\le 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.