alpar@1: /* ZEBRA, Who Owns the Zebra? */ alpar@1: alpar@1: /* Written in GNU MathProg by Andrew Makhorin */ alpar@1: alpar@1: ######################################################################## alpar@1: # The Zebra Puzzle is a well-known logic puzzle. alpar@1: # alpar@1: # It is often called "Einstein's Puzzle" or "Einstein's Riddle" alpar@1: # because it is said to have been invented by Albert Einstein as a boy, alpar@1: # with the common claim that Einstein said "only 2 percent of the alpar@1: # world's population can solve". It is also sometimes attributed to alpar@1: # Lewis Carroll. However, there is no known evidence for Einstein's or alpar@1: # Carroll's authorship. alpar@1: # alpar@1: # There are several versions of this puzzle. The version below is alpar@1: # quoted from the first known publication in Life International alpar@1: # magazine on December 17, 1962. alpar@1: # alpar@1: # 1. There are five houses. alpar@1: # 2. The Englishman lives in the red house. alpar@1: # 3. The Spaniard owns the dog. alpar@1: # 4. Coffee is drunk in the green house. alpar@1: # 5. The Ukrainian drinks tea. alpar@1: # 6. The green house is immediately to the right of the ivory house. alpar@1: # 7. The Old Gold smoker owns snails. alpar@1: # 8. Kools are smoked in the yellow house. alpar@1: # 9. Milk is drunk in the middle house. alpar@1: # 10. The Norwegian lives in the first house. alpar@1: # 11. The man who smokes Chesterfields lives in the house next to the alpar@1: # man with the fox. alpar@1: # 12. Kools are smoked in the house next to the house where the horse alpar@1: # is kept. alpar@1: # 13. The Lucky Strike smoker drinks orange juice. alpar@1: # 14. The Japanese smokes Parliaments. alpar@1: # 15. The Norwegian lives next to the blue house. alpar@1: # alpar@1: # Now, who drinks water? Who owns the zebra? alpar@1: # alpar@1: # In the interest of clarity, it must be added that each of the five alpar@1: # houses is painted a different color, and their inhabitants are of alpar@1: # different national extractions, own different pets, drink different alpar@1: # beverages and smoke different brands of American cigarettes. One alpar@1: # other thing: In statement 6, right means your right. alpar@1: # alpar@1: # (From Wikipedia, the free encyclopedia.) alpar@1: ######################################################################## alpar@1: alpar@1: set HOUSE := { 1..5 }; alpar@1: alpar@1: set COLOR := { "blue", "green", "ivory", "red", "yellow" }; alpar@1: alpar@1: set NATIONALITY := { "Englishman", "Japanese", "Norwegian", "Spaniard", alpar@1: "Ukranian" }; alpar@1: alpar@1: set DRINK := { "coffee", "milk", "orange_juice", "tea", "water" }; alpar@1: alpar@1: set SMOKE := { "Chesterfield", "Kools", "Lucky_Strike", "Old_Gold", alpar@1: "Parliament" }; alpar@1: alpar@1: set PET := { "dog", "fox", "horse", "snails", "zebra" }; alpar@1: alpar@1: var color{HOUSE, COLOR}, binary; alpar@1: c1{h in HOUSE}: sum{c in COLOR} color[h,c] = 1; alpar@1: c2{c in COLOR}: sum{h in HOUSE} color[h,c] = 1; alpar@1: alpar@1: var nationality{HOUSE, NATIONALITY}, binary; alpar@1: n1{h in HOUSE}: sum{n in NATIONALITY} nationality[h,n] = 1; alpar@1: n2{n in NATIONALITY}: sum{h in HOUSE} nationality[h,n] = 1; alpar@1: alpar@1: var drink{HOUSE, DRINK}, binary; alpar@1: d1{h in HOUSE}: sum{d in DRINK} drink[h,d] = 1; alpar@1: d2{d in DRINK}: sum{h in HOUSE} drink[h,d] = 1; alpar@1: alpar@1: var smoke{HOUSE, SMOKE}, binary; alpar@1: s1{h in HOUSE}: sum{s in SMOKE} smoke[h,s] = 1; alpar@1: s2{s in SMOKE}: sum{h in HOUSE} smoke[h,s] = 1; alpar@1: alpar@1: var pet{HOUSE, PET}, binary; alpar@1: p1{h in HOUSE}: sum{p in PET} pet[h,p] = 1; alpar@1: p2{p in PET}: sum{h in HOUSE} pet[h,p] = 1; alpar@1: alpar@1: /* the Englishman lives in the red house */ alpar@1: f2{h in HOUSE}: nationality[h,"Englishman"] = color[h,"red"]; alpar@1: alpar@1: /* the Spaniard owns the dog */ alpar@1: f3{h in HOUSE}: nationality[h,"Spaniard"] = pet[h,"dog"]; alpar@1: alpar@1: /* coffee is drunk in the green house */ alpar@1: f4{h in HOUSE}: drink[h,"coffee"] = color[h,"green"]; alpar@1: alpar@1: /* the Ukrainian drinks tea */ alpar@1: f5{h in HOUSE}: nationality[h,"Ukranian"] = drink[h,"tea"]; alpar@1: alpar@1: /* the green house is immediately to the right of the ivory house */ alpar@1: f6{h in HOUSE}: alpar@1: color[h,"green"] = if h = 1 then 0 else color[h-1,"ivory"]; alpar@1: alpar@1: /* the Old Gold smoker owns snails */ alpar@1: f7{h in HOUSE}: smoke[h,"Old_Gold"] = pet[h,"snails"]; alpar@1: alpar@1: /* Kools are smoked in the yellow house */ alpar@1: f8{h in HOUSE}: smoke[h,"Kools"] = color[h,"yellow"]; alpar@1: alpar@1: /* milk is drunk in the middle house */ alpar@1: f9: drink[3,"milk"] = 1; alpar@1: alpar@1: /* the Norwegian lives in the first house */ alpar@1: f10: nationality[1,"Norwegian"] = 1; alpar@1: alpar@1: /* the man who smokes Chesterfields lives in the house next to the man alpar@1: with the fox */ alpar@1: f11{h in HOUSE}: alpar@1: (1 - smoke[h,"Chesterfield"]) + alpar@1: (if h = 1 then 0 else pet[h-1,"fox"]) + alpar@1: (if h = 5 then 0 else pet[h+1,"fox"]) >= 1; alpar@1: alpar@1: /* Kools are smoked in the house next to the house where the horse is alpar@1: kept */ alpar@1: f12{h in HOUSE}: alpar@1: (1 - smoke[h,"Kools"]) + alpar@1: (if h = 1 then 0 else pet[h-1,"horse"]) + alpar@1: (if h = 5 then 0 else pet[h+1,"horse"]) >= 1; alpar@1: alpar@1: /* the Lucky Strike smoker drinks orange juice */ alpar@1: f13{h in HOUSE}: smoke[h,"Lucky_Strike"] = drink[h,"orange_juice"]; alpar@1: alpar@1: /* the Japanese smokes Parliaments */ alpar@1: f14{h in HOUSE}: nationality[h,"Japanese"] = smoke[h,"Parliament"]; alpar@1: alpar@1: /* the Norwegian lives next to the blue house */ alpar@1: f15{h in HOUSE}: alpar@1: (1 - nationality[h,"Norwegian"]) + alpar@1: (if h = 1 then 0 else color[h-1,"blue"]) + alpar@1: (if h = 5 then 0 else color[h+1,"blue"]) >= 1; alpar@1: alpar@1: solve; alpar@1: alpar@1: printf "\n"; alpar@1: printf "HOUSE COLOR NATIONALITY DRINK SMOKE PET\n"; alpar@1: for {h in HOUSE} alpar@1: { printf "%5d", h; alpar@1: printf{c in COLOR: color[h,c]} " %-6s", c; alpar@1: printf{n in NATIONALITY: nationality[h,n]} " %-11s", n; alpar@1: printf{d in DRINK: drink[h,d]} " %-12s", d; alpar@1: printf{s in SMOKE: smoke[h,s]} " %-12s", s; alpar@1: printf{p in PET: pet[h,p]} " %-6s", p; alpar@1: printf "\n"; alpar@1: } alpar@1: printf "\n"; alpar@1: alpar@1: end;