Zebra puzzle: Difference between revisions

13,893 bytes removed ,  1 month ago
m
m (syntax highlighting fixup automation)
(17 intermediate revisions by 6 users not shown)
Line 15:
:#   The green house is immediately to the left of the white house.
:#   They drink coffee in the green house.
:#   The man who smokes Pall Mall has birdsa bird.
:#   In the yellow house they smoke Dunhill.
:#   In the middle house they drink milk.
:#   The Norwegian lives in the first house.
:#   The man who smokes Blend-smoker lives in the house next to the house with catsa cat.
:#   In a house next to the house where they havewith a horse, they smoke Dunhill.
:#   The man who smokes Blue Master drinks beer.
:#   The German smokes Prince.
Line 26:
:#   They drink water in a house next to the house where they smoke Blend.
 
<br>The question is, who owns the zebra? For clarity, each of the five houses is painted a different color, and their inhabitants are of different nationalities, own different pets, drink different beverages and smoke different brands of cigarettes.
<br>The question is, who owns the zebra?
 
Additionally, list the solution for all the houses.
Line 305:
solutions: 1
</pre>
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">on zebraPuzzle()
-- From statement 10, the Norwegian lives in the first house,
-- so from statement 15, the blue house must be the second one.
-- From these and statements 5, 6, and 9, the green and white houses can only be the 4th & 5th,
-- and the Englishman's red house (statement 2) must be the middle one, where (9) they drink
-- milk. This only leaves the first house to claim the yellow colour and the Dunhill smokers
-- (statement 8), which means the second house must have the the horse (statement 12).
-- Initialise the house data accordingly.
set mv to missing value
set streetTemplate to {¬
{resident:"Norwegian", colour:"yellow", pet:mv, drink:mv, smoke:"Dunhill"}, ¬
{resident:mv, colour:"blue", pet:"horse", drink:mv, smoke:mv}, ¬
{resident:"Englishman", colour:"red", pet:mv, drink:"milk", smoke:mv}, ¬
{resident:mv, colour:"green", pet:mv, drink:"coffee", smoke:mv}, ¬
{resident:mv, colour:"white", pet:mv, drink:mv, smoke:mv} ¬
}
-- Test all permutations of the remaining values.
set solutions to {}
set drinkPermutations to {{"beer", "water"}, {"water", "beer"}}
set residentPermutations to {{"Swede", "Dane", "German"}, {"Swede", "German", "Dane"}, ¬
{"Dane", "German", "Swede"}, {"Dane", "Swede", "German"}, ¬
{"German", "Swede", "Dane"}, {"German", "Dane", "Swede"}}
set petPermutations to {{"birds", "cats", "ZEBRA"}, {"birds", "ZEBRA", "cats"}, ¬
{"cats", "ZEBRA", "birds"}, {"cats", "birds", "ZEBRA"}, ¬
{"ZEBRA", "birds", "cats"}, {"ZEBRA", "cats", "birds"}}
set smokePermutations to {{"Pall Mall", "Blend", "Blue Master"}, {"Pall Mall", "Blue Master", "Blend"}, ¬
{"Blend", "Blue Master", "Pall Mall"}, {"Blend", "Pall Mall", "Blue Master"}, ¬
{"Blue Master", "Pall Mall", "Blend"}, {"Blue Master", "Blend", "Pall Mall"}}
repeat with residentPerm in residentPermutations
-- Properties associated with resident.
copy streetTemplate to sTemplate2
set {r, OK} to {0, true}
repeat with h in {2, 4, 5} -- House numbers with unknown residents.
set thisHouse to sTemplate2's item h
set r to r + 1
set thisResident to residentPerm's item r
if (thisResident is "Swede") then
if (thisHouse's pet is not mv) then
set OK to false
exit repeat
end if
set thisHouse's pet to "dog"
else if (thisResident is "Dane") then
if (thisHouse's drink is not mv) then
set OK to false
exit repeat
end if
set thisHouse's drink to "tea"
else
set thisHouse's smoke to "Prince"
end if
set thisHouse's resident to thisResident
end repeat
-- Properties associated with cigarette brand.
if (OK) then
repeat with smokePerm in smokePermutations
-- Fit in this permutation of smokes.
copy sTemplate2 to sTemplate3
set s to 0
repeat with thisHouse in sTemplate3
if (thisHouse's smoke is mv) then
set s to s + 1
set thisHouse's smoke to smokePerm's item s
end if
end repeat
repeat with drinkPerm in drinkPermutations
-- Try to fit this permutation of drinks.
copy sTemplate3 to sTemplate4
set {d, OK} to {0, true}
repeat with h from 1 to 5
set thisHouse to sTemplate4's item h
if (thisHouse's drink is mv) then
set d to d + 1
set thisDrink to drinkPerm's item d
if (((thisDrink is "beer") and (thisHouse's smoke is not "Blue Master")) or ¬
((thisDrink is "water") and not ¬
(((h > 1) and (sTemplate4's item (h - 1)'s smoke is "Blend")) or ¬
((h < 5) and (sTemplate4's item (h + 1)'s smoke is "Blend"))))) then
set OK to false
exit repeat
end if
set thisHouse's drink to thisDrink
end if
end repeat
if (OK) then
repeat with petPerm in petPermutations
-- Try to fit this permutation of pets.
copy sTemplate4 to sTemplate5
set {p, OK} to {0, true}
repeat with h from 1 to 5
set thisHouse to sTemplate5's item h
if (thisHouse's pet is mv) then
set p to p + 1
set thisPet to petPerm's item p
if ((thisPet is "birds") and (thisHouse's smoke is not "Pall Mall")) or ¬
((thisPet is "cats") and not ¬
(((h > 1) and (sTemplate5's item (h - 1)'s smoke is "Blend")) or ¬
((h < 5) and (sTemplate5's item (h + 1)'s smoke is "Blend")))) then
set OK to false
exit repeat
end if
set thisHouse's pet to thisPet
end if
end repeat
if (OK) then set end of solutions to sTemplate5
end repeat
end if
end repeat
end repeat
end if
end repeat
set solutionCount to (count solutions)
set owners to {}
repeat with thisSolution in solutions
repeat with thisHouse in thisSolution
if (thisHouse's pet is "zebra") then
set owners's end to thisHouse's resident
exit repeat
end if
end repeat
end repeat
return {zebraOwners:owners, numberOfSolutions:solutionCount, solutions:solutions}
end zebraPuzzle
 
zebraPuzzle()</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">{zebraOwners:{"German"}, numberOfSolutions:1, solutions:{{{resident:"Norwegian", colour:"yellow", pet:"cats", drink:"water", smoke:"Dunhill"}, {resident:"Dane", colour:"blue", pet:"horse", drink:"tea", smoke:"Blend"}, {resident:"Englishman", colour:"red", pet:"birds", drink:"milk", smoke:"Pall Mall"}, {resident:"German", colour:"green", pet:"ZEBRA", drink:"coffee", smoke:"Prince"}, {resident:"Swede", colour:"white", pet:"dog", drink:"beer", smoke:"Blue Master"}}}}</syntaxhighlight>
 
=={{header|AutoHotkey}}==
Line 4,087 ⟶ 4,219:
 
===Constraint Programming Version===
Using the rules from https://github.com/SWI-Prolog/bench/blob/master/programs/zebra.pl
<syntaxhighlight lang="julia"># Julia 1.4
using JuMP
Line 4,979 ⟶ 5,111:
The German owns the zebra.</pre>
 
=={{header|PariPARI/GpGP}}==
<syntaxhighlight lang="pari/gpnone">
perm(arr) = {
n=#arr;i=n-1;
Line 5,177 ⟶ 5,309:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<syntaxhighlight lang="phix">
<span style="color: #008080;">enum</span> <span style="color: #000000;">Colour</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">Nationality</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">Drink</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">Smoke</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">Pet</span>
--enum colour, nationality, drink, smoke, pet -- (now implicit)
<span style="color: #008080;">constant</span> <span style="color: #000000;">Colours</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"red"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"white"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"green"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"yellow"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"blue"</span><span style="color: #0000FF;">},</span>
enum red,white,green,yellow,blue
<span style="color: #000000;">Nationalities</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"English"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Swede"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Dane"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Norwegian"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"German"</span><span style="color: #0000FF;">},</span>
enum English,Swede,Dane,Norwegian,German
<span style="color: #000000;">Drinks</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"tea"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"coffee"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"milk"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"beer"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"water"</span><span style="color: #0000FF;">},</span>
enum tea,coffee,milk,beer,water
<span style="color: #000000;">Smokes</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"Pall Mall"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Dunhill"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Blend"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Blue Master"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Prince"</span><span style="color: #0000FF;">},</span>
enum PallMall,Dunhill,Blend,BlueMaster,Prince
<span style="color: #000000;">Pets</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"dog"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"birds"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"cats"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"horse"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"zebra"</span><span style="color: #0000FF;">},</span>
enum dog,birds,cats,horse,zebra
<span style="color: #000000;">Sets</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">Colours</span><span style="color: #0000FF;">,</span><span style="color: #000000;">Nationalities</span><span style="color: #0000FF;">,</span><span style="color: #000000;">Drinks</span><span style="color: #0000FF;">,</span><span style="color: #000000;">Smokes</span><span style="color: #0000FF;">,</span><span style="color: #000000;">Pets</span><span style="color: #0000FF;">}</span>
 
constant colours = {"red","white","green","yellow","blue"},
<span style="color: #008080;">constant</span> <span style="color: #000000;">tagset5</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- {1,2,3,4,5}, oft-permuted</span>
nationalities = {"English","Swede","Dane","Norwegian","German"},
<span style="color: #004080;">sequence</span> <span style="color: #000000;">perm</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tagset5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- perm[1] is Colour of each house, etc</span>
drinks = {"tea","coffee","milk","beer","water"},
smokes = {"Pall Mall","Dunhill","Blend","Blue Master","Prince"},
<span style="color: #008080;">function</span> <span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">name</span><span style="color: #0000FF;">)</span>
pets = {"dog","birds","cats","horse","zebra"},
<span style="color: #008080;">return</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">name</span><span style="color: #0000FF;">,</span><span style="color: #000000;">Sets</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]),</span><span style="color: #000000;">perm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
sets = {colours,nationalities,drinks,smokes,pets}
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
constant p5 = permutes(tagset(5)), -- all permutes of {1,2,3,4,5},
<span style="color: #008080;">function</span> <span style="color: #000000;">left_of</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">h1</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">h2</span><span style="color: #0000FF;">)</span>
lp = length(p5) -- (== factorial(5), ie 120)
<span style="color: #008080;">return</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">h1</span><span style="color: #0000FF;">-</span><span style="color: #000000;">h2</span><span style="color: #0000FF;">)==</span><span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
// In the following, c1,c2 are indexes to p5, for colour..pet,
// and v1,v2 are from their corresponding enums, so eg p5[c1]
<span style="color: #008080;">function</span> <span style="color: #000000;">next_to</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">h1</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">h2</span><span style="color: #0000FF;">)</span>
// might be {1,4,3,2,5} for the colours of 1..5 and finding
<span style="color: #008080;">return</span> <span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #000000;">h1</span><span style="color: #0000FF;">-</span><span style="color: #000000;">h2</span><span style="color: #0000FF;">)==</span><span style="color: #000000;">1</span>
// v1 in that gives us a house number. Checking the specified
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
// condition, eg [h] == green && [h+1] == white is then easy.
 
<span style="color: #008080;">procedure</span> <span style="color: #000000;">print_house</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
function left_of(integer c1, v1, c2, v2)
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d:%s,%s,%s,%s,%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span>
integer h = find(v1,p5[c1])
<span style="color: #000000;">Colours</span><span style="color: #0000FF;">[</span><span style="color: #000000;">perm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">Colour</span><span style="color: #0000FF;">][</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]],</span>
return h<=4 and p5[c2][h+1]=v2
<span style="color: #000000;">Nationalities</span><span style="color: #0000FF;">[</span><span style="color: #000000;">perm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">Nationality</span><span style="color: #0000FF;">][</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]],</span>
end function
<span style="color: #000000;">Drinks</span><span style="color: #0000FF;">[</span><span style="color: #000000;">perm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">Drink</span><span style="color: #0000FF;">][</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]],</span>
 
<span style="color: #000000;">Smokes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">perm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">Smoke</span><span style="color: #0000FF;">][</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]],</span>
function same_house(integer c1, v1, c2, v2)
<span style="color: #000000;">Pets</span><span style="color: #0000FF;">[</span><span style="color: #000000;">perm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">Pet</span><span style="color: #0000FF;">][</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]]})</span>
integer h = find(v1,p5[c1])
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
return p5[c2][h]=v2
end function
<span style="color: #004080;">integer</span> <span style="color: #000000;">solutions</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">solperms</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
function next_to(integer c1, v1, c2, v2)
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
integer h1 = find(v1,p5[c1]),
<span style="color: #008080;">constant</span> <span style="color: #000000;">factorial5</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">factorial</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
h2 = find(v2,p5[c2])
<span style="color: #008080;">for</span> <span style="color: #000000;">C</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">factorial5</span> <span style="color: #008080;">do</span>
return abs(h1-h2)=1
<span style="color: #000000;">perm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">Colour</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">permute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">C</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tagset5</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #008080;">if</span> <span style="color: #000000;">left_of</span><span style="color: #0000FF;">(</span><span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Colour</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"green"</span><span style="color: #0000FF;">),</span><span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Colour</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"white"</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">N</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">factorial5</span> <span style="color: #008080;">do</span>
procedure print_house(integer n, sequence perm)
<span style="color: #000000;">perm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">Nationality</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">permute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">N</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tagset5</span><span style="color: #0000FF;">)</span>
sequence args = {n}
<span style="color: #008080;">if</span> <span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Nationality</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Norwegian"</span><span style="color: #0000FF;">)==</span><span style="color: #000000;">1</span>
for i,p in perm do
<span style="color: #008080;">and</span> <span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Nationality</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"English"</span><span style="color: #0000FF;">)==</span><span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Colour</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"red"</span><span style="color: #0000FF;">)</span>
args = append(args,sets[i][p5[p][n]])
<span style="color: #008080;">and</span> <span style="color: #000000;">next_to</span><span style="color: #0000FF;">(</span><span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Nationality</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Norwegian"</span><span style="color: #0000FF;">),</span><span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Colour</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"blue"</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">then</span>
end for
<span style="color: #008080;">for</span> <span style="color: #000000;">D</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">factorial5</span> <span style="color: #008080;">do</span>
printf(1,"House %d: %|7s %|10s %|6s %|12s %=6s\n",args)
<span style="color: #000000;">perm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">Drink</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">permute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">D</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tagset5</span><span style="color: #0000FF;">)</span>
end procedure
<span style="color: #008080;">if</span> <span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Nationality</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Dane"</span><span style="color: #0000FF;">)==</span><span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Drink</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"tea"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">and</span> <span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Drink</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"coffee"</span><span style="color: #0000FF;">)==</span><span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Colour</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"green"</span><span style="color: #0000FF;">)</span>
integer ns = 0
<span style="color: #008080;">and</span> <span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Drink</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"milk"</span><span style="color: #0000FF;">)==</span><span style="color: #000000;">3</span> <span style="color: #008080;">then</span>
atom t0 = time()
<span style="color: #008080;">for</span> <span style="color: #000000;">S</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">factorial5</span> <span style="color: #008080;">do</span>
for colour=1 to lp do
<span style="color: #000000;">perm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">Smoke</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">permute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">S</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tagset5</span><span style="color: #0000FF;">)</span>
if left_of(colour,green,colour,white) then
<span style="color: #008080;">if</span> <span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Colour</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"yellow"</span><span style="color: #0000FF;">)==</span><span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Smoke</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Dunhill"</span><span style="color: #0000FF;">)</span>
for nationality=1 to lp do
<span style="color: #008080;">and</span> <span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Nationality</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"German"</span><span style="color: #0000FF;">)==</span><span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Smoke</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Prince"</span><span style="color: #0000FF;">)</span>
if p5[nationality][1]=Norwegian -- Norwegian lives in 1st house
<span style="color: #008080;">and</span> <span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Smoke</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Blue Master"</span><span style="color: #0000FF;">)==</span><span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Drink</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"beer"</span><span style="color: #0000FF;">)</span>
and same_house(nationality,English,colour,red)
<span style="color: #008080;">and</span> <span style="color: #000000;">next_to</span><span style="color: #0000FF;">(</span><span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Drink</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"water"</span><span style="color: #0000FF;">),</span><span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Smoke</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Blend"</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">then</span>
and next_to(nationality,Norwegian,colour,blue) then
<span style="color: #008080;">for</span> <span style="color: #000000;">P</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">factorial5</span> <span style="color: #008080;">do</span>
for drink=1 to lp do
<span style="color: #000000;">perm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">Pet</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">permute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">P</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tagset5</span><span style="color: #0000FF;">)</span>
if same_house(nationality,Dane,drink,tea)
<span style="color: #008080;">if</span> <span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Nationality</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Swede"</span><span style="color: #0000FF;">)==</span><span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Pet</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"dog"</span><span style="color: #0000FF;">)</span>
and same_house(drink,coffee,colour,green)
<span style="color: #008080;">and</span> <span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Smoke</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Pall Mall"</span><span style="color: #0000FF;">)==</span><span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Pet</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"birds"</span><span style="color: #0000FF;">)</span>
and p5[drink][3]=milk then -- middle house drinks milk
<span style="color: #008080;">and</span> <span style="color: #000000;">next_to</span><span style="color: #0000FF;">(</span><span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Smoke</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Blend"</span><span style="color: #0000FF;">),</span><span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Pet</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"cats"</span><span style="color: #0000FF;">))</span>
for smoke=1 to lp do
<span style="color: #008080;">and</span> <span style="color: #000000;">next_to</span><span style="color: #0000FF;">(</span><span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Pet</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"horse"</span><span style="color: #0000FF;">),</span><span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Smoke</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Dunhill"</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">then</span>
if same_house(colour,yellow,smoke,Dunhill)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">5</span> <span style="color: #008080;">do</span>
and same_house(nationality,German,smoke,Prince)
<span style="color: #000000;">print_house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
and <span style="color: #008080;">end</span> <span style="color: #008080;">for</span>same_house(smoke,BlueMaster,drink,beer)
and next_to(drink,water,smoke,Blend) then
<span style="color: #000000;">solutions</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
for pet=1 to lp do
<span style="color: #000000;">solperms</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">solperms</span><span style="color: #0000FF;">,</span><span style="color: #000000;">perm</span><span style="color: #0000FF;">)</span>
if <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>same_house(nationality,Swede,pet,dog)
<span style="color: #008080;">end</span> <spanand style="color: #008080;">for</span>same_house(smoke,PallMall,pet,birds)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> and next_to(smoke,Blend,pet,cats)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> and next_to(pet,horse,smoke,Dunhill) then
<span style="color: #008080;">end</span> <span style for i="color:1 to 5 #008080;">if</span>do
print_house(i,{colour,nationality,drink,smoke,pet})
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
integer z = p5[nationality][find(zebra,p5[pet])]
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1,"The %s owns the Zebra\n",{nationalities[z]})
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
ns += 1
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d solution%s found (%3.3fs).\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">solutions</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">solutions</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"s"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">})</span>
end for
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">solperms</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
end if
<span style="color: #000000;">perm</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">solperms</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
end for
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The %s owns the Zebra\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">Nationalities</span><span style="color: #0000FF;">[</span><span style="color: #000000;">house</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Pet</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"zebra"</span><span style="color: #0000FF;">)]})</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<!--</syntaxhighlight>-->
end if
end for
end if
end for
printf(1,"%d solution%s found (%3.3fs).\n",{ns,iff(ns>1,"s",""),time()-t0})
</syntaxhighlight>
{{out}}
<pre>
House 1: yellow, Norwegian, water, Dunhill, cats
House 2: blue, Dane, tea, Blend, horse
House 3: red, English, milk, Pall Mall, birds
House 4: green, German, coffee, Prince, zebra
House 5: white, Swede, beer, Blue Master, dog
1 solution found (0.016s).
The German owns the Zebra
1 solution found (0.000s).
</pre>
 
Line 5,853 ⟶ 5,991:
Using 'python-constraint': http://labix.org/python-constraint,
Original source code is 'ECLiPSe ( http://eclipseclp.org/ )' example: http://eclipseclp.org/examples/zebra.ecl.txt
<syntaxhighlight lang="python">from constraint import *'''
Improved version: Instead of simple variables (a, b),
more intelligible and readable (?) variables are employed.
'''
def doc1():
'''
There are five houses.
The English man lives in the red house.
The Swede has a dog.
The Dane drinks tea.
The green house is immediately to the left of the white house.
They drink coffee in the green house.
The man who smokes Pall Mall has a bird.
In the yellow house they smoke Dunhill.
In the middle house they drink milk.
The Norwegian lives in the first house.
The man who smokes Blend lives in the house next to the house with a cat.
In a house next to the house where they have a horse, they smoke Dunhill.
The man who smokes Blue Master drinks beer.
The German smokes Prince.
The Norwegian lives next to the blue house.
They drink water in a house next to the house where they smoke Blend.
 
The solution:
problem = Problem()
 
Nation: Norwegian Dane = ["Englishman", "Spaniard", "Japanese", German "Ukrainian", "Norwegian" ]Swede
Color: = ["Red", Yellow "Green", Blue "White", "Yellow",Red "Blue" Green ] White
Smoke: Dunhill Blend PallMall Prince BlueMaster
Smoke = ["Oldgold", "Kools", "Chesterfield", "Luckystrike", "Parliament"]
Pet = ["Dog", Pet: "Snails", "Fox", Cat "Horse", " Bird Zebra" ] Dog
Drink: = ["Tea", Water "Coffee", "Milk", Tea "Orangejuice", "Water"Milk Coffee ]Beer
'''
 
import os
print(os.system('cls') if os.name == 'nt' else os.system('clear'))
 
print(doc1.__doc__)
input('<key>')
 
from constraint import *
 
p = Problem()
 
Nation = ['Englishman', 'Dane', 'German', 'Norwegian', 'Swede']
Color = [ 'Blue', 'Green', 'Red', 'White', 'Yellow']
Smoke = [ 'Blend', 'BlueMaster', 'Dunhill', 'PallMall', 'Prince']
Pet = [ 'Bird', 'Cat', 'Dog', 'Horse', 'Zebra']
Drink = [ 'Beer', 'Coffee', 'Milk', 'Tea', 'Water']
 
# add variables: house numbers 1 to 5
problemp.addVariables(Nation, range(1,5+16))
problemp.addVariables(Color, range(1,5+16))
problemp.addVariables(Smoke, range(1,5+16))
problemp.addVariables(Pet, range(1,5+16))
problemp.addVariables(Drink, range(1,5+16))
 
# add constraint: the values in each list are exclusive
problemp.addConstraint(AllDifferentConstraint(), Nation)
problemp.addConstraint(AllDifferentConstraint(), Color)
problemp.addConstraint(AllDifferentConstraint(), Smoke)
problemp.addConstraint(AllDifferentConstraint(), Pet)
problemp.addConstraint(AllDifferentConstraint(), Drink)
 
# add constraint: actual constraints
# The English man lives in the red house.
problem.addConstraint(lambda a, b: a == b, ["Englishman", "Red" ])
p.addConstraint(
problem.addConstraint(lambda a, b: a == b, ["Spaniard", "Dog" ])
lambda house_englishman, red:
problem.addConstraint(lambda a, b: a == b, ["Green", "Coffee" ])
house_englishman is red,
problem.addConstraint(lambda a, b: a == b, ["Ukrainian", "Tea" ])
['Englishman', 'Red'])
problem.addConstraint(lambda a, b: a == b + 1, ["Green", "White" ])
 
problem.addConstraint(lambda a, b: a == b, ["Oldgold", "Snails" ])
# The Swede has a dog.
problem.addConstraint(lambda a, b: a == b, ["Yellow", "Kools" ])
p.addConstraint(
problem.addConstraint(lambda a: a == 3, ["Milk" ])
lambda pet_swede, dog:
problem.addConstraint(lambda a: a == 1, ["Norwegian" ])
problem.addConstraint(lambda a, b: a ==pet_swede is dog, b - 1 or a == b + 1, ["Chesterfield", "Fox" ])
['Swede', 'Dog'])
problem.addConstraint(lambda a, b: a == b - 1 or a == b + 1, ["Kools", "Horse" ])
 
problem.addConstraint(lambda a, b: a == b, ["Luckystrike", "Orangejuice"])
# The Dane drinks tea.
problem.addConstraint(lambda a, b: a == b, ["Japanese", "Parliament" ])
p.addConstraint(
problem.addConstraint(lambda a, b: a == b - 1 or a == b + 1, ["Norwegian", "Blue" ])
lambda drink_dane, tea:
drink_dane is tea,
['Dane', 'Tea'])
 
# The green house is immediately to the left of the white house.
p.addConstraint(
lambda green_house, white_house:
# Houses 1 .. 5 -> green house is 1 lower than white house
green_house is white_house - 1,
['Green', 'White'])
 
# They drink coffee in the green house.
p.addConstraint(
lambda drink_green_house, coffee:
drink_green_house is coffee,
['Green', 'Coffee'])
 
# The man who smokes Pall Mall has a bird.
p.addConstraint(
lambda pet_of_pallmall_smoker, a_bird:
pet_of_pallmall_smoker is a_bird,
['PallMall', 'Bird'])
 
# In the yellow house they smoke Dunhill.
p.addConstraint(
lambda owner_yellow_house, dunhill_smoker:
owner_yellow_house is dunhill_smoker,
['Yellow', 'Dunhill'])
 
# In the middle house they drink milk.
p.addConstraint(
lambda house_number_milk_drinker:
house_number_milk_drinker is 3,
['Milk'])
 
# The Norwegian lives in the first house.
p.addConstraint(
lambda house_number_norwegian:
house_number_norwegian is 1,
['Norwegian'])
 
# The man who smokes Blend lives in the house next to the house with a cat.
p.addConstraint(
lambda house_number_blend_smoker, number_of_house_with_cat:
# next -> housenumber +/- 1
house_number_blend_smoker is number_of_house_with_cat + 1 or
house_number_blend_smoker is number_of_house_with_cat - 1,
['Blend', 'Cat'])
 
# In a house next to the house where they have a horse, they smoke Dunhill.
p.addConstraint(
lambda house_number_dunhill_smoker, number_of_house_with_horse:
# next -> housenumber +/- 1
house_number_dunhill_smoker is number_of_house_with_horse + 1 or
house_number_dunhill_smoker is number_of_house_with_horse - 1,
['Dunhill', 'Horse'])
 
# The man who smokes Blue Master drinks beer.
p.addConstraint(
lambda drink_bluemaster_smoker, beer:
drink_bluemaster_smoker is beer,
['BlueMaster', 'Beer'])
 
# The German smokes Prince.
p.addConstraint(
lambda prince_smoker, german:
prince_smoker is german,
['Prince', 'German'])
 
# The Norwegian lives next to the blue house.
p.addConstraint(
lambda house_number_norwegian, house_number_blue_house:
house_number_norwegian is house_number_blue_house + 1 or
house_number_norwegian is house_number_blue_house - 1,
['Norwegian', 'Blue'])
 
# They drink water in a house next to the house where they smoke Blend.
p.addConstraint(
lambda house_number_water_drinker, house_number_blend_smoker:
house_number_water_drinker is house_number_blend_smoker + 1 or
house_number_water_drinker is house_number_blend_smoker - 1,
['Water', 'Blend'])
 
# get solution
sol = problemp.getSolution()
 
# print the answers
nation = ["'Nation"' if i == 0 else ""'' for i in range(6)]
color = ["'Color"' if i == 0 else ""'' for i in range(6)]
smoke = ["'Smoke"' if i == 0 else ""'' for i in range(6)]
pet = ["'Pet"' if i == 0 else ""'' for i in range(6)]
drink = ["'Drink"' if i == 0 else ""'' for i in range(6)]
 
for n in Nation:
for n in Nation: nation[sol[n]] = n
for n in Color: color[sol[n]] = n
for n in colorSmoke: smoke[sol[n]] = n
for n in Smoke Pet: pet[sol[n]] = n
for n in smokeDrink: drink[sol[n]] = n
 
for n in Pet:
# put the answers in the correct order of solution
pet[sol[n]] = n
print('\n\n', 'The calculated solution:', '\n')
for n in Drink:
 
drink[sol[n]] = n
for dr in [nation, color, smoke, pet, drink]:
print(f'{r[0]:>13s}: ', end='')
print("%6s: %14s%14s%14s%14s%14s" % (d[0], d[1], d[2], d[3], d[4], d[5]))
for i in range(1, 6):
print(f'{r[i]:>14s}',end='')
print()
print()
</syntaxhighlight>
Output:
<pre>Nation: Nation: Norwegian Ukrainian Dane Englishman SpaniardGerman JapaneseSwede
Color: Color: Yellow Yellow Blue Blue Red Green White GreenWhite
Smoke: Smoke: Dunhill Kools Chesterfield Blend PallMall Oldgold Luckystrike Prince ParliamentBlueMaster
Pet: Pet: Fox Cat Horse Horse Snails Bird Zebra Dog ZebraDog
Drink: Drink: Water Water Tea Tea Milk Orangejuice Coffee Coffee</pre> Beer
</pre>
 
=={{header|R}}==
Line 7,476 ⟶ 7,739:
The German owns the Zebra</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">type HouseSet = []House
struct House {
n Nationality
Line 7,767 ⟶ 8,030:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var colors = ["Red", "Green", "White", "Yellow", "Blue"]
1

edit