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 houseTemplatesstreetTemplate to {¬
{resident:"Norwegian", colour:"yellow", pet:mv, drink:mv, smoke:"Dunhill"}, ¬
{resident:mv, colour:"blue", pet:"horse", drink:mv, smoke:mv}, ¬
Line 324:
}
-- Test all combinationspermutations of the remaining values.
set solutions to {}
set drinkPermutations to {{"beer", "water"}, {"water", "beer"}}
Line 338:
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 364 ⟶ 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 smokePermthisHouse in smokePermutationssTemplate3
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 d +{0, 1true}
repeat with h from end1 ifto 5
set thisHouse's smoke to smokePermsTemplate4's item sh
if (thisHouse's drink is mv) then
set d endto ifd + 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 (testHousessTemplate4's item (h - 1)'s smoke is "Blend")) or ¬
((h < 5) and (testHousessTemplate4'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 ¬
if ((thisPet is "birdscats") 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
set thisHouse's drink set OK to thisDrinkfalse
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
557

edits