Zebra puzzle: Difference between revisions
Content added Content deleted
m (→{{header|AppleScript}}: Removed unnecessary line, hard-coded permutations, removed permutations handler.) |
m (→{{header|AppleScript}}: Optimised with less template copying.) |
||
Line 316: | Line 316: | ||
-- Initialise the house data accordingly. |
-- Initialise the house data accordingly. |
||
set mv to missing value |
set mv to missing value |
||
set |
set streetTemplate to {¬ |
||
{resident:"Norwegian", colour:"yellow", pet:mv, drink:mv, smoke:"Dunhill"}, ¬ |
{resident:"Norwegian", colour:"yellow", pet:mv, drink:mv, smoke:"Dunhill"}, ¬ |
||
{resident:mv, colour:"blue", pet:"horse", drink:mv, smoke:mv}, ¬ |
{resident:mv, colour:"blue", pet:"horse", drink:mv, smoke:mv}, ¬ |
||
Line 324: | Line 324: | ||
} |
} |
||
-- Test all |
-- Test all permutations of the remaining values. |
||
set solutions to {} |
set solutions to {} |
||
set drinkPermutations to {{"beer", "water"}, {"water", "beer"}} |
set drinkPermutations to {{"beer", "water"}, {"water", "beer"}} |
||
Line 338: | Line 338: | ||
repeat with residentPerm in residentPermutations |
repeat with residentPerm in residentPermutations |
||
-- Properties associated with resident. |
-- Properties associated with resident. |
||
copy streetTemplate to sTemplate2 |
|||
⚫ | |||
copy houseTemplates to intermediateTemplates |
|||
⚫ | |||
repeat with h in {2, 4, 5} -- House numbers with unknown residents. |
repeat with h in {2, 4, 5} -- House numbers with unknown residents. |
||
set thisHouse to |
set thisHouse to sTemplate2's item h |
||
set r to r + 1 |
set r to r + 1 |
||
set thisResident to residentPerm's item r |
set thisResident to residentPerm's item r |
||
Line 364: | Line 363: | ||
-- Properties associated with cigarette brand. |
-- Properties associated with cigarette brand. |
||
if (OK) then |
if (OK) then |
||
repeat with |
repeat with smokePerm in smokePermutations |
||
-- Fit in this permutation of smokes. |
|||
copy sTemplate2 to sTemplate3 |
|||
⚫ | |||
set s to 0 |
|||
⚫ | |||
copy intermediateTemplates to testHouses |
|||
if (thisHouse's smoke is mv) then |
|||
set s to s + 1 |
|||
set thisHouse's smoke to smokePerm's item s |
|||
end if |
|||
⚫ | |||
⚫ | |||
repeat with drinkPerm in drinkPermutations |
|||
-- Try to fit this permutation of drinks. |
|||
copy sTemplate3 to sTemplate4 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end if |
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 |
|||
if ((thisPet is "birds") and (thisHouse's smoke is not "Pall Mall")) or ¬ |
|||
set thisHouse to sTemplate5's item h |
|||
if (thisHouse's pet is mv) then |
|||
set p to p + 1 |
|||
set |
set thisPet to petPerm's item p |
||
if ((thisPet is "birds") and (thisHouse's smoke is not "Pall Mall")) or ¬ |
|||
⚫ | |||
(((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 |
|||
⚫ | |||
exit repeat |
|||
end if |
|||
set thisHouse's pet to thisPet |
|||
end if |
end if |
||
end repeat |
|||
end |
if (OK) then set end of solutions to sTemplate5 |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end repeat |
end repeat |
||
end if |
|||
⚫ | |||
end repeat |
end repeat |
||
end repeat |
end repeat |