Zebra puzzle: Difference between revisions
Content added Content deleted
m (→{{header|AppleScript}}: Minor correction in output code.) |
m (→{{header|AppleScript}}: Removed unnecessary line, hard-coded permutations, removed permutations handler.) |
||
Line 307: | Line 307: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
<syntaxhighlight lang="applescript">on |
<syntaxhighlight lang="applescript">on zebraPuzzle() |
||
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, |
-- From statement 10, the Norwegian lives in the first house, |
||
-- so from statement 15, the blue house must be the second one. |
-- so from statement 15, the blue house must be the second one. |
||
Line 406: | Line 327: | ||
set solutions to {} |
set solutions to {} |
||
set drinkPermutations to {{"beer", "water"}, {"water", "beer"}} |
set drinkPermutations to {{"beer", "water"}, {"water", "beer"}} |
||
set |
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"}} |
|||
⚫ | |||
{"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. |
-- Properties associated with resident. |
||
set r to 0 |
set r to 0 |
||
Line 485: | Line 413: | ||
set solutionCount to (count solutions) |
set solutionCount to (count solutions) |
||
if (solutionCount = 0) then return {zebraOwners:{}, numberOfSolutions:0, solutions:{}} |
|||
set owners to {} |
set owners to {} |
||
repeat with thisSolution in solutions |
repeat with thisSolution in solutions |