Zebra puzzle: Difference between revisions
m
→{{header|AppleScript}}: Optimised with less template copying.
m (→{{header|AppleScript}}: Removed unnecessary line, hard-coded permutations, removed permutations handler.) |
m (→{{header|AppleScript}}: Optimised with less template copying.) |
||
Line 316:
-- Initialise the house data accordingly.
set mv to missing value
set
{resident:"Norwegian", colour:"yellow", pet:mv, drink:mv, smoke:"Dunhill"}, ¬
{resident:mv, colour:"blue", pet:"horse", drink:mv, smoke:mv}, ¬
Line 324:
}
-- Test all
set solutions to {}
set drinkPermutations to {{"beer", "water"}, {"water", "beer"}}
Line 338:
repeat with residentPerm in residentPermutations
-- Properties associated with resident.
set {r, OK} to {0, true}▼
▲ set OK to true
repeat with h in {2, 4, 5} -- House numbers with unknown residents.
set thisHouse to
set r to r + 1
set thisResident to residentPerm's item r
Line 364 ⟶ 363:
-- Properties associated with cigarette brand.
if (OK) then
repeat with
copy sTemplate2 to sTemplate3
repeat with smokePerm in smokePermutations▼
if (thisHouse's
set
end
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
end if
end
if (OK)
repeat with
copy
set {p,
repeat with h from 1 to 5
set thisHouse
set p to
set
(((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 (OK) then set end
▲ 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 repeat
end repeat
end repeat
|