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 πšŽπš•πšŽπš–πšŽπš—πš constraints with variables in their tables (i.e.,Β the table of an πšŽπš•πšŽπš–πšŽπš—πš constraint corresponds to its second argument). It consists of creating for each house i (1≀i≀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 πšŠπš•πš•πšπš’πšπšπšŽπš›πšŽπš—πš 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∈{π‘Ÿπ‘’π‘‘,π‘”π‘Ÿπ‘’π‘’π‘›,π‘–π‘£π‘œπ‘Ÿπ‘¦,π‘¦π‘’π‘™π‘™π‘œπ‘€,𝑏𝑙𝑒𝑒} we create a variable I c that corresponds to the index of the house having this colour. For each variable I c , an πšŽπš•πšŽπš–πšŽπš—πš 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 πšŽπš•πšŽπš–πšŽπš—πš constraints by the following πš’πš—πšŸπšŽπš›πšœπšŽ constraint:

  • πš’πš—πšŸπšŽπš›πšœπšŽπš’πš—πšπšŽπš‘-1𝚜𝚞𝚌𝚌-C 1 πš™πš›πšŽπš-I π‘Ÿπ‘’π‘‘ ,πš’πš—πšπšŽπš‘-2𝚜𝚞𝚌𝚌-C 2 πš™πš›πšŽπš-I π‘”π‘Ÿπ‘’π‘’π‘› ,πš’πš—πšπšŽπš‘-3𝚜𝚞𝚌𝚌-C 3 πš™πš›πšŽπš-I π‘–π‘£π‘œπ‘Ÿπ‘¦ ,πš’πš—πšπšŽπš‘-4𝚜𝚞𝚌𝚌-C 4 πš™πš›πšŽπš-I π‘¦π‘’π‘™π‘™π‘œπ‘€ ,πš’πš—πšπšŽπš‘-5𝚜𝚞𝚌𝚌-C 5 πš™πš›πšŽπš-I 𝑏𝑙𝑒𝑒

For each possible nationality n∈{π‘’π‘›π‘”π‘™π‘–π‘ β„Žπ‘šπ‘Žπ‘›,π‘ π‘π‘Žπ‘›π‘–π‘Žπ‘Ÿπ‘‘,π‘’π‘˜π‘Ÿπ‘Žπ‘–π‘›π‘–π‘Žπ‘›,π‘›π‘œπ‘Ÿπ‘€π‘’π‘”π‘–π‘Žπ‘›, π‘—π‘Žπ‘π‘Žπ‘›π‘’π‘ π‘’} 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 πšŽπš•πšŽπš–πšŽπš—πš constraint links it to the variables N 1 ,N 2 ,N 3 ,N 4 ,N 5 giving the nationality associated with each house:

  • πΈπ‘›π‘”π‘™π‘–π‘ β„Žπ‘šπ‘Žπ‘›=1, π‘†π‘π‘Žπ‘›π‘–π‘Žπ‘Ÿπ‘‘=2, π‘ˆπ‘˜π‘Ÿπ‘Žπ‘–π‘›π‘–π‘Žπ‘›=3, π‘π‘œπ‘Ÿπ‘€π‘’π‘”π‘–π‘Žπ‘›=4, π½π‘Žπ‘π‘Žπ‘›π‘’π‘ π‘’=5,

  • πšŽπš•πšŽπš–πšŽπš—πš(I π‘’π‘›π‘”π‘™π‘–π‘ β„Žπ‘šπ‘Žπ‘› ,〈N 1 ,N 2 ,N 3 ,N 4 ,N 5 βŒͺ,πΈπ‘›π‘”π‘™π‘–π‘ β„Žπ‘šπ‘Žπ‘›),

  • πšŽπš•πšŽπš–πšŽπš—πš(I π‘ π‘π‘Žπ‘›π‘–π‘Žπ‘Ÿπ‘‘ ,〈N 1 ,N 2 ,N 3 ,N 4 ,N 5 βŒͺ,π‘†π‘π‘Žπ‘›π‘–π‘Žπ‘Ÿπ‘‘),

  • πšŽπš•πšŽπš–πšŽπš—πš(I π‘’π‘˜π‘Ÿπ‘Žπ‘–π‘›π‘–π‘Žπ‘› ,〈N 1 ,N 2 ,N 3 ,N 4 ,N 5 βŒͺ,π‘ˆπ‘˜π‘Ÿπ‘Žπ‘–π‘›π‘–π‘Žπ‘›),

  • πšŽπš•πšŽπš–πšŽπš—πš(I π‘›π‘œπ‘Ÿπ‘€π‘’π‘”π‘–π‘Žπ‘› ,〈N 1 ,N 2 ,N 3 ,N 4 ,N 5 βŒͺ,π‘π‘œπ‘Ÿπ‘€π‘’π‘”π‘–π‘Žπ‘›),

  • πšŽπš•πšŽπš–πšŽπš—πš(I π‘—π‘Žπ‘π‘Žπ‘›π‘’π‘ π‘’ ,〈N 1 ,N 2 ,N 3 ,N 4 ,N 5 βŒͺ,π½π‘Žπ‘π‘Žπ‘›π‘’π‘ π‘’).

Again we can replace the five previous πšŽπš•πšŽπš–πšŽπš—πš constraints by the following πš’πš—πšŸπšŽπš›πšœπšŽ constraint:

  • πš’πš—πšŸπšŽπš›πšœπšŽπš’πš—πšπšŽπš‘-1𝚜𝚞𝚌𝚌-N 1 πš™πš›πšŽπš-I π‘’π‘›π‘”π‘™π‘–π‘ β„Žπ‘šπ‘Žπ‘› ,πš’πš—πšπšŽπš‘-2𝚜𝚞𝚌𝚌-N 2 πš™πš›πšŽπš-I π‘ π‘π‘Žπ‘›π‘–π‘Žπ‘Ÿπ‘‘ ,πš’πš—πšπšŽπš‘-3𝚜𝚞𝚌𝚌-N 3 πš™πš›πšŽπš-I π‘’π‘˜π‘Ÿπ‘Žπ‘–π‘›π‘–π‘Žπ‘› ,πš’πš—πšπšŽπš‘-4𝚜𝚞𝚌𝚌-N 4 πš™πš›πšŽπš-I π‘›π‘œπ‘Ÿπ‘€π‘’π‘”π‘–π‘Žπ‘› ,πš’πš—πšπšŽπš‘-5𝚜𝚞𝚌𝚌-N 5 πš™πš›πšŽπš-I π‘—π‘Žπ‘π‘Žπ‘›π‘’π‘ π‘’

For each possible preferred pet a∈{π‘‘π‘œπ‘”,π‘ π‘›π‘Žπ‘–π‘™,π‘“π‘œπ‘₯,β„Žπ‘œπ‘Ÿπ‘ π‘’,π‘§π‘’π‘π‘Ÿπ‘Ž} 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 πšŽπš•πšŽπš–πšŽπš—πš 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 πšŽπš•πšŽπš–πšŽπš—πš constraints by the following πš’πš—πšŸπšŽπš›πšœπšŽ constraint:

  • πš’πš—πšŸπšŽπš›πšœπšŽπš’πš—πšπšŽπš‘-1𝚜𝚞𝚌𝚌-A 1 πš™πš›πšŽπš-I π‘‘π‘œπ‘” ,πš’πš—πšπšŽπš‘-2𝚜𝚞𝚌𝚌-A 2 πš™πš›πšŽπš-I π‘ π‘›π‘Žπ‘–π‘™ ,πš’πš—πšπšŽπš‘-3𝚜𝚞𝚌𝚌-A 3 πš™πš›πšŽπš-I π‘“π‘œπ‘₯ ,πš’πš—πšπšŽπš‘-4𝚜𝚞𝚌𝚌-A 4 πš™πš›πšŽπš-I β„Žπ‘œπ‘Ÿπ‘ π‘’ ,πš’πš—πšπšŽπš‘-5𝚜𝚞𝚌𝚌-A 5 πš™πš›πšŽπš-I π‘§π‘’π‘π‘Ÿπ‘Ž

For each possible preferred beverage d∈{π‘π‘œπ‘“π‘“π‘’π‘’,π‘‘π‘’π‘Ž,π‘šπ‘–π‘™π‘˜,π‘œπ‘Ÿπ‘Žπ‘›π‘”π‘’_𝑗𝑒𝑖𝑐𝑒,π‘€π‘Žπ‘‘π‘’π‘Ÿ} 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 πšŽπš•πšŽπš–πšŽπš—πš 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 πšŽπš•πšŽπš–πšŽπš—πš constraints by the following πš’πš—πšŸπšŽπš›πšœπšŽ constraint:

  • πš’πš—πšŸπšŽπš›πšœπšŽπš’πš—πšπšŽπš‘-1𝚜𝚞𝚌𝚌-D 1 πš™πš›πšŽπš-I π‘π‘œπ‘“π‘“π‘’π‘’ ,πš’πš—πšπšŽπš‘-2𝚜𝚞𝚌𝚌-D 2 πš™πš›πšŽπš-I π‘‘π‘’π‘Ž ,πš’πš—πšπšŽπš‘-3𝚜𝚞𝚌𝚌-D 3 πš™πš›πšŽπš-I π‘šπ‘–π‘™π‘˜ ,πš’πš—πšπšŽπš‘-4𝚜𝚞𝚌𝚌-D 4 πš™πš›πšŽπš-I π‘œπ‘Ÿπ‘Žπ‘›π‘”π‘’_𝑗𝑒𝑖𝑐𝑒 ,πš’πš—πšπšŽπš‘-5𝚜𝚞𝚌𝚌-D 5 πš™πš›πšŽπš-I π‘€π‘Žπ‘‘π‘’π‘Ÿ

For each possible preferred brand of American cigarettes b∈{π‘œπ‘™π‘‘_π‘”π‘œπ‘™π‘‘,π‘˜π‘œπ‘œπ‘™, π‘β„Žπ‘’π‘ π‘‘π‘’π‘Ÿπ‘“π‘–π‘’π‘™π‘‘,π‘™π‘’π‘π‘˜π‘¦_π‘ π‘‘π‘Ÿπ‘–π‘˜π‘’,π‘π‘Žπ‘Ÿπ‘™π‘–π‘Žπ‘šπ‘’π‘›π‘‘} 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 πšŽπš•πšŽπš–πšŽπš—πš 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 πšŽπš•πšŽπš–πšŽπš—πš constraints by the following πš’πš—πšŸπšŽπš›πšœπšŽ constraint:

  • πš’πš—πšŸπšŽπš›πšœπšŽπš’πš—πšπšŽπš‘-1𝚜𝚞𝚌𝚌-B 1 πš™πš›πšŽπš-I π‘œπ‘™π‘‘_π‘”π‘œπ‘™π‘‘ ,πš’πš—πšπšŽπš‘-2𝚜𝚞𝚌𝚌-B 2 πš™πš›πšŽπš-I π‘˜π‘œπ‘œπ‘™ ,πš’πš—πšπšŽπš‘-3𝚜𝚞𝚌𝚌-B 3 πš™πš›πšŽπš-I π‘β„Žπ‘’π‘ π‘‘π‘’π‘Ÿπ‘“π‘–π‘’π‘™π‘‘ ,πš’πš—πšπšŽπš‘-4𝚜𝚞𝚌𝚌-B 4 πš™πš›πšŽπš-I π‘™π‘’π‘π‘˜π‘¦_π‘ π‘‘π‘Ÿπ‘–π‘˜π‘’ ,πš’πš—πšπšŽπš‘-5𝚜𝚞𝚌𝚌-B 5 πš™πš›πšŽπš-I π‘π‘Žπ‘Ÿπ‘™π‘–π‘Žπ‘šπ‘’π‘›π‘‘

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

  • I π‘’π‘›π‘”π‘™π‘–π‘ β„Žπ‘šπ‘Žπ‘› =I π‘Ÿπ‘’π‘‘ Β (the Englishman lives in the red house).

  • I π‘ π‘π‘Žπ‘›π‘–π‘Žπ‘Ÿπ‘‘ =I π‘‘π‘œπ‘” Β (the Spaniard owns the dog).

  • I π‘π‘œπ‘“π‘“π‘’π‘’ =I π‘”π‘Ÿπ‘’π‘’π‘› Β (coffee is drunk in the green house).

  • I π‘’π‘˜π‘Ÿπ‘Žπ‘–π‘›π‘–π‘Žπ‘› =I π‘‘π‘’π‘Ž Β (the Ukrainian drinks tea).

  • I π‘”π‘Ÿπ‘’π‘’π‘› =I π‘–π‘£π‘œπ‘Ÿπ‘¦ +1Β (the green house is immediately to the right of the ivory house).

  • I π‘œπ‘™π‘‘_π‘”π‘œπ‘™π‘‘ =I π‘ π‘›π‘Žπ‘–π‘™ Β (the Old Gold smoker owns snails).

  • I π‘˜π‘œπ‘œπ‘™ =I π‘¦π‘’π‘™π‘™π‘œπ‘€ Β (kools are smoked in the yellow house).

  • I π‘šπ‘–π‘™π‘˜ =3Β (milk is drunk in the middle house).

  • I π‘›π‘œπ‘Ÿπ‘€π‘’π‘”π‘–π‘Žπ‘› =1Β (the Norwegian lives in the first house).

  • |I π‘β„Žπ‘’π‘ π‘‘π‘’π‘Ÿπ‘“π‘–π‘’π‘™π‘‘ -I π‘“π‘œπ‘₯ |=1Β (the man who smokes Chesterfields lives in the house next to the man with the fox).

  • |I π‘˜π‘œπ‘œπ‘™ -I β„Žπ‘œπ‘Ÿπ‘ π‘’ |=1Β (kools are smoked in the house next to the house where the horse is kept).

  • I π‘™π‘’π‘π‘˜π‘¦_π‘ π‘‘π‘Ÿπ‘–π‘˜π‘’ =I π‘œπ‘Ÿπ‘Žπ‘›π‘”π‘’_𝑗𝑒𝑖𝑐𝑒 Β (the Lucky Strike smoker drinks orange juice).

  • I π‘—π‘Žπ‘π‘Žπ‘›π‘’π‘ π‘’ =I π‘π‘Žπ‘Ÿπ‘™π‘–π‘Žπ‘šπ‘’π‘›π‘‘ Β (the Japanese smokes Parliaments).

  • |I π‘›π‘œπ‘Ÿπ‘€π‘’π‘”π‘–π‘Žπ‘› -I 𝑏𝑙𝑒𝑒 |=1Β (the Norwegian lives next to the blue house).

Now note that variables C i , N i , A i , D i , B i (1≀i≀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 πšŠπš•πš•πšπš’πšπšπšŽπš›πšŽπš—πš constraints on these variables by the following πšŠπš•πš•πšπš’πšπšπšŽπš›πšŽπš—πš 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≀i≀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.