Zebra puzzle: Difference between revisions
Content added Content deleted
(→{{header|Vlang}}: Rename "Vlang" in "V (Vlang)") |
(Added AppleScript.) |
||
Line 305: | Line 305: | ||
solutions: 1 |
solutions: 1 |
||
</pre> |
</pre> |
||
=={{header|AppleScript}}== |
|||
<syntaxhighlight lang="applescript">on allPermutations(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. |
|||
-- 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 houseTemplates 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 combinations of the remaining values. |
|||
set solutions to {} |
|||
set drinkPermutations to {{"beer", "water"}, {"water", "beer"}} |
|||
set petPermutations to allPermutations({"birds", "cats", "ZEBRA"}) |
|||
set smokePermutations to allPermutations({"Pall Mall", "Blend", "Blue Master"}) |
|||
repeat with residentPerm in allPermutations({"Swede", "Dane", "German"}) |
|||
-- Properties associated with resident. |
|||
set r to 0 |
|||
copy houseTemplates to intermediateTemplates |
|||
set OK to true |
|||
repeat with h in {2, 4, 5} -- House numbers with unknown residents. |
|||
set thisHouse to intermediateTemplates'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 drinkPerm in drinkPermutations |
|||
repeat with petPerm in petPermutations |
|||
repeat with smokePerm in smokePermutations |
|||
set {d, p, s} to {0, 0, 0} |
|||
copy intermediateTemplates to testHouses |
|||
-- Fit in this combination of smokes. |
|||
repeat with thisHouse in testHouses |
|||
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 |
|||
-- Try to fit the other properties accordingly. |
|||
set OK to true |
|||
repeat with h from 1 to 5 |
|||
set thisHouse to testHouses'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 (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 pet to thisPet |
|||
end if |
|||
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 |
|||
if (OK) then set end of solutions to testHouses |
|||
end repeat |
|||
end repeat |
|||
end repeat |
|||
end if |
|||
end repeat |
|||
set solutionCount to (count solutions) |
|||
if (solutionCount = 0) then return {zebraOwners:missing value, numberOfSolutions:0, solutions:{}} |
|||
set owners to {} |
|||
repeat with thisHouse in solutions's first item |
|||
if (thisHouse's pet is "zebra") then |
|||
set owners's end to thisHouse's resident |
|||
end if |
|||
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}}== |
=={{header|AutoHotkey}}== |