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 houseTemplates to {¬
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 combinations of the remaining values.
-- 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.
set r to 0
copy streetTemplate to sTemplate2
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.
repeat with h in {2, 4, 5} -- House numbers with unknown residents.
set thisHouse to intermediateTemplates's item h
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 drinkPerm in drinkPermutations
repeat with smokePerm in smokePermutations
repeat with petPerm in petPermutations
-- Fit in this permutation of smokes.
copy sTemplate2 to sTemplate3
repeat with smokePerm in smokePermutations
set {d, p, s} to {0, 0, 0}
set s to 0
repeat with thisHouse in sTemplate3
copy intermediateTemplates to testHouses
-- Fit in this combination of smokes.
if (thisHouse's smoke is mv) then
repeat with thisHouse in testHouses
set s to s + 1
if (thisHouse's smoke is mv) then
set thisHouse's smoke to smokePerm's item s
set s to s + 1
end if
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 {0, true}
repeat with h from 1 to 5
set thisHouse to sTemplate4's item h
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 (sTemplate4's item (h - 1)'s smoke is "Blend")) or ¬
((h < 5) and (sTemplate4's item (h + 1)'s smoke is "Blend"))))) then
set OK to false
exit repeat
end if
end if
end repeat
set thisHouse's drink to thisDrink
-- Try to fit the other properties accordingly.
end if
set OK to true
end repeat
repeat with h from 1 to 5
if (OK) then
set thisHouse to testHouses's item h
repeat with petPerm in petPermutations
if (thisHouse's pet is mv) then
-- Try to fit this permutation of pets.
set p to p + 1
copy sTemplate4 to sTemplate5
set thisPet to petPerm's item p
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 ¬
((thisPet is "cats") and not ¬
set thisHouse to sTemplate5's item h
(((h > 1) and (testHouses's item (h - 1)'s smoke is "Blend")) or ¬
if (thisHouse's pet is mv) then
((h < 5) and (testHouses's item (h + 1)'s smoke is "Blend")))) then
set p to p + 1
set OK to false
set thisPet to petPerm's item p
exit repeat
if ((thisPet is "birds") and (thisHouse's smoke is not "Pall Mall")) or ¬
((thisPet is "cats") and not ¬
(((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 OK to false
exit repeat
end if
set thisHouse's pet to thisPet
end if
end if
set thisHouse's pet to thisPet
end repeat
end if
if (OK) then set end of 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 repeat
if (OK) then set end of solutions to testHouses
end if
end repeat
end repeat
end repeat
end repeat
end repeat