Zebra puzzle: Difference between revisions

21,981 bytes removed ,  1 month ago
m
m (→‎{{header|AppleScript}}: Minor correction in output code.)
(14 intermediate revisions by 5 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 307:
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">on allPermutationszebraPuzzle(theList)
script o
property workList : missing value
property permutations : {}
property r : (count theList)
property |r-1| : r - 1
property |r-2| : r - 2
property p : 1
on prmt(l)
set evenCount to ((r - l) mod 2 = 1)
repeat until (l = |r-2|)
set |l+1| to l + 1
prmt(|l+1|)
if (evenCount) then
repeat with swapIndex from r to |l+1| + 1 by -1
tell item l of my workList
set item l of my workList to item swapIndex of my workList
set item swapIndex of my workList to it
end tell
prmt(|l+1|)
end repeat
set swapIndex to |l+1|
else
repeat (r - |l+1|) times
tell item l of my workList
set item l of my workList to item r of my workList
set item r of my workList to it
end tell
prmt(|l+1|)
end repeat
set swapIndex to r
end if
tell item l of my workList
set item l of my workList to item swapIndex of my workList
set item swapIndex of my workList to it
end tell
set l to |l+1|
set evenCount to (not evenCount)
end repeat
copy workList to item p of my permutations
set {v1, v2, v3} to items |r-2| thru r of my workList
set item |r-1| of my workList to v3
set item r of my workList to v2
copy workList to item (p + 1) of my permutations
set item |r-2| of my workList to v2
set item r of my workList to v1
copy workList to item (p + 2) of my permutations
set item |r-1| of my workList to v1
set item r of my workList to v3
copy workList to item (p + 3) of my permutations
set item |r-2| of my workList to v3
set item r of my workList to v2
copy workList to item (p + 4) of my permutations
set item |r-1| of my workList to v2
set item r of my workList to v1
copy workList to item (p + 5) of my permutations
set p to p + 6
end prmt
end script
if (o's r < 3) then
copy theList to the beginning of o's permutations
if (o's r is 2) then set the end of o's permutations to theList's reverse
else
copy theList to o's workList
set o's permutations to {missing value, missing value}
repeat with i from 3 to (count theList)
set temp to o's permutations
repeat (i - 1) times
set o's permutations to o's permutations & temp
end repeat
end repeat
o's prmt(1)
end if
return o's permutations
end allPermutations
 
on zebraPuzzle()
-- From statement 10, the Norwegian lives in the first house,
-- so from statement 15, the blue house must be the second one.
Line 395 ⟶ 316:
-- Initialise the house data accordingly.
set mv to missing value
set houseTemplatesstreetTemplate to {¬
{resident:"Norwegian", colour:"yellow", pet:mv, drink:mv, smoke:"Dunhill"}, ¬
{resident:mv, colour:"blue", pet:"horse", drink:mv, smoke:mv}, ¬
Line 403 ⟶ 324:
}
-- Test all combinationspermutations of the remaining values.
set solutions to {}
set drinkPermutations to {{"beer", "water"}, {"water", "beer"}}
set petPermutationsresidentPermutations to allPermutations({{"birdsSwede", "catsDane", "ZEBRAGerman"}), {"Swede", "German", "Dane"}, ¬
{"Dane", "German", "Swede"}, {"Dane", "Swede", "German"}, ¬
set smokePermutations to allPermutations({"Pall Mall", "Blend", "Blue Master"})
repeat with residentPerm in allPermutations({"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.
setcopy rstreetTemplate to 0sTemplate2
set {r, OK} to {0, true}
copy houseTemplates to intermediateTemplates
set OK to true
repeat with h in {2, 4, 5} -- House numbers with unknown residents.
set thisHouse to intermediateTemplatessTemplate2's item h
set r to r + 1
set thisResident to residentPerm's item r
Line 436 ⟶ 363:
-- Properties associated with cigarette brand.
if (OK) then
repeat with drinkPermsmokePerm in drinkPermutationssmokePermutations
repeat-- with petPermFit in petPermutationsthis permutation of smokes.
copy sTemplate2 to sTemplate3
repeat with smokePerm in smokePermutations
set {d, p, s} to {0, 0, 0}
repeat with thisHouse in sTemplate3
copy intermediateTemplates to testHouses
if (thisHouse's --smoke Fitis inmv) this combination of smokes.then
repeatset withs thisHouseto ins testHouses+ 1
set if (thisHouse's smoke isto mv)smokePerm's thenitem s
end set s to s + 1if
end repeat
set thisHouse's smoke to smokePerm's item s
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
end repeat set thisHouse's drink to thisDrink
--end Try to fit the other properties accordingly.if
end set OK to truerepeat
if (OK) repeat with h from 1 to 5then
repeat with setpetPerm thisHousein to testHouses's item hpetPermutations
if-- (thisHouse'sTry petto isfit mv)this thenpermutation of pets.
copy set psTemplate4 to p + 1sTemplate5
set {p, set thisPetOK} to petPerm's{0, item ptrue}
repeat with h from 1 to 5
if ((thisPet is "birds") and (thisHouse's smoke is not "Pall Mall")) or ¬
set thisHouse ((thisPetto issTemplate5's "cats")item and not ¬h
if (((h > 1) and (testHousesthisHouse's item (h - 1)'s smokepet is "Blend"mv)) or ¬then
set p to ((h < 5) and (testHouses's item (hp + 1)'s smoke is "Blend")))) then
set OKthisPet to falsepetPerm's item p
exitif repeat((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 set thisHouse's pet to thisPetrepeat
if (OK) then set end ifof solutions to sTemplate5
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 (testHouses's item (h - 1)'s smoke is "Blend")) or ¬
((h < 5) and (testHouses'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
end if (OK) then set end of solutions to testHouses
end repeat
end repeat
end repeat
Line 485 ⟶ 421:
set solutionCount to (count solutions)
if (solutionCount = 0) then return {zebraOwners:{}, numberOfSolutions:0, solutions:{}}
set owners to {}
repeat with thisSolution in solutions
Line 4,284 ⟶ 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 5,176 ⟶ 5,111:
The German owns the zebra.</pre>
 
=={{header|PariPARI/GpGP}}==
<syntaxhighlight lang="pari/gpnone">
perm(arr) = {
n=#arr;i=n-1;
Line 5,374 ⟶ 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 6,050 ⟶ 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,964 ⟶ 8,030:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var colors = ["Red", "Green", "White", "Yellow", "Blue"]
1

edit