1 /* ZEBRA, Who Owns the Zebra? */
3 /* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
5 ########################################################################
6 # The Zebra Puzzle is a well-known logic puzzle.
8 # It is often called "Einstein's Puzzle" or "Einstein's Riddle"
9 # because it is said to have been invented by Albert Einstein as a boy,
10 # with the common claim that Einstein said "only 2 percent of the
11 # world's population can solve". It is also sometimes attributed to
12 # Lewis Carroll. However, there is no known evidence for Einstein's or
13 # Carroll's authorship.
15 # There are several versions of this puzzle. The version below is
16 # quoted from the first known publication in Life International
17 # magazine on December 17, 1962.
19 # 1. There are five houses.
20 # 2. The Englishman lives in the red house.
21 # 3. The Spaniard owns the dog.
22 # 4. Coffee is drunk in the green house.
23 # 5. The Ukrainian drinks tea.
24 # 6. The green house is immediately to the right of the ivory house.
25 # 7. The Old Gold smoker owns snails.
26 # 8. Kools are smoked in the yellow house.
27 # 9. Milk is drunk in the middle house.
28 # 10. The Norwegian lives in the first house.
29 # 11. The man who smokes Chesterfields lives in the house next to the
31 # 12. Kools are smoked in the house next to the house where the horse
33 # 13. The Lucky Strike smoker drinks orange juice.
34 # 14. The Japanese smokes Parliaments.
35 # 15. The Norwegian lives next to the blue house.
37 # Now, who drinks water? Who owns the zebra?
39 # In the interest of clarity, it must be added that each of the five
40 # houses is painted a different color, and their inhabitants are of
41 # different national extractions, own different pets, drink different
42 # beverages and smoke different brands of American cigarettes. One
43 # other thing: In statement 6, right means your right.
45 # (From Wikipedia, the free encyclopedia.)
46 ########################################################################
48 set HOUSE := { 1..5 };
50 set COLOR := { "blue", "green", "ivory", "red", "yellow" };
52 set NATIONALITY := { "Englishman", "Japanese", "Norwegian", "Spaniard",
55 set DRINK := { "coffee", "milk", "orange_juice", "tea", "water" };
57 set SMOKE := { "Chesterfield", "Kools", "Lucky_Strike", "Old_Gold",
60 set PET := { "dog", "fox", "horse", "snails", "zebra" };
62 var color{HOUSE, COLOR}, binary;
63 c1{h in HOUSE}: sum{c in COLOR} color[h,c] = 1;
64 c2{c in COLOR}: sum{h in HOUSE} color[h,c] = 1;
66 var nationality{HOUSE, NATIONALITY}, binary;
67 n1{h in HOUSE}: sum{n in NATIONALITY} nationality[h,n] = 1;
68 n2{n in NATIONALITY}: sum{h in HOUSE} nationality[h,n] = 1;
70 var drink{HOUSE, DRINK}, binary;
71 d1{h in HOUSE}: sum{d in DRINK} drink[h,d] = 1;
72 d2{d in DRINK}: sum{h in HOUSE} drink[h,d] = 1;
74 var smoke{HOUSE, SMOKE}, binary;
75 s1{h in HOUSE}: sum{s in SMOKE} smoke[h,s] = 1;
76 s2{s in SMOKE}: sum{h in HOUSE} smoke[h,s] = 1;
78 var pet{HOUSE, PET}, binary;
79 p1{h in HOUSE}: sum{p in PET} pet[h,p] = 1;
80 p2{p in PET}: sum{h in HOUSE} pet[h,p] = 1;
82 /* the Englishman lives in the red house */
83 f2{h in HOUSE}: nationality[h,"Englishman"] = color[h,"red"];
85 /* the Spaniard owns the dog */
86 f3{h in HOUSE}: nationality[h,"Spaniard"] = pet[h,"dog"];
88 /* coffee is drunk in the green house */
89 f4{h in HOUSE}: drink[h,"coffee"] = color[h,"green"];
91 /* the Ukrainian drinks tea */
92 f5{h in HOUSE}: nationality[h,"Ukranian"] = drink[h,"tea"];
94 /* the green house is immediately to the right of the ivory house */
96 color[h,"green"] = if h = 1 then 0 else color[h-1,"ivory"];
98 /* the Old Gold smoker owns snails */
99 f7{h in HOUSE}: smoke[h,"Old_Gold"] = pet[h,"snails"];
101 /* Kools are smoked in the yellow house */
102 f8{h in HOUSE}: smoke[h,"Kools"] = color[h,"yellow"];
104 /* milk is drunk in the middle house */
105 f9: drink[3,"milk"] = 1;
107 /* the Norwegian lives in the first house */
108 f10: nationality[1,"Norwegian"] = 1;
110 /* the man who smokes Chesterfields lives in the house next to the man
113 (1 - smoke[h,"Chesterfield"]) +
114 (if h = 1 then 0 else pet[h-1,"fox"]) +
115 (if h = 5 then 0 else pet[h+1,"fox"]) >= 1;
117 /* Kools are smoked in the house next to the house where the horse is
120 (1 - smoke[h,"Kools"]) +
121 (if h = 1 then 0 else pet[h-1,"horse"]) +
122 (if h = 5 then 0 else pet[h+1,"horse"]) >= 1;
124 /* the Lucky Strike smoker drinks orange juice */
125 f13{h in HOUSE}: smoke[h,"Lucky_Strike"] = drink[h,"orange_juice"];
127 /* the Japanese smokes Parliaments */
128 f14{h in HOUSE}: nationality[h,"Japanese"] = smoke[h,"Parliament"];
130 /* the Norwegian lives next to the blue house */
132 (1 - nationality[h,"Norwegian"]) +
133 (if h = 1 then 0 else color[h-1,"blue"]) +
134 (if h = 5 then 0 else color[h+1,"blue"]) >= 1;
139 printf "HOUSE COLOR NATIONALITY DRINK SMOKE PET\n";
142 printf{c in COLOR: color[h,c]} " %-6s", c;
143 printf{n in NATIONALITY: nationality[h,n]} " %-11s", n;
144 printf{d in DRINK: drink[h,d]} " %-12s", d;
145 printf{s in SMOKE: smoke[h,s]} " %-12s", s;
146 printf{p in PET: pet[h,p]} " %-6s", p;