Zebra puzzle: Difference between revisions
Content added Content deleted
(Added alternative Tailspin solution) |
|||
Line 6,848: | Line 6,848: | ||
=={{header|Tailspin}}== |
=={{header|Tailspin}}== |
||
===General solver with relational algebra=== |
|||
A general solver for this type of puzzle, using relational algebra. This solver can also be used for "Dinesman's multiple-dwelling problem" |
A general solver for this type of puzzle, using relational algebra. This solver can also be used for "Dinesman's multiple-dwelling problem" |
||
<lang tailspin> |
<lang tailspin> |
||
Line 6,960: | Line 6,961: | ||
No more solutions |
No more solutions |
||
</pre> |
|||
===Manual "Norvig" solution=== |
|||
{{trans|C#}} |
|||
The streaming syntax of Tailspin lends itself naturally to this solution |
|||
<lang tailspin> |
|||
templates permutations |
|||
when <=1> do [1] ! |
|||
otherwise |
|||
def n: $; |
|||
templates expand |
|||
def p: $; |
|||
1..$n -> \(def k: $; |
|||
[$p(1..$k-1)..., $n, $p($k..last)...] !\) ! |
|||
end expand |
|||
$n - 1 -> permutations -> expand ! |
|||
end permutations |
|||
def permutationsOf5: [5 -> permutations]; |
|||
def nationalities: ['Englishman', 'Swede', 'Dane', 'Norwegian', 'German']; |
|||
def colours: ['red', 'green', 'white', 'yellow', 'blue']; |
|||
def pets: ['dog', 'birds', 'cats', 'horse', 'zebra']; |
|||
def drinks: ['tea', 'coffee', 'milk', 'beer', 'water']; |
|||
def smokes: ['Pall Mall', 'Dunhill', 'Blend', 'Blue Master', 'Prince']; |
|||
$permutationsOf5... -> $colours($) -> [$... -> {colour: $}] |
|||
-> \(<[(<{colour: <='green'>}>:<{colour: <='white'>}>)]> $! \) |
|||
-> \(def current: $; |
|||
$permutationsOf5... -> $nationalities($) -> \[i]({$current($i), nationality: $}! \) ! |
|||
\) |
|||
-> \(<?($(1) <{nationality: <='Norwegian'>}>)> $! \) |
|||
-> \(<[<{nationality: <='Englishman'>, colour: <='red'>}>]> $! \) |
|||
-> \(<[(<{nationality: <='Norwegian'>}>:<{colour: <='blue'>}>)] | [(<{colour: <='blue'>}>:<{nationality: <='Norwegian'>}>)]> $! \) |
|||
-> \(def current: $; |
|||
$permutationsOf5... -> $drinks($) -> \[i]({$current($i), drink: $}! \) ! |
|||
\) |
|||
-> \(<?($(3) <{drink: <='milk'>}>)> $! \) |
|||
-> \(<[<{drink: <='coffee'>, colour: <='green'>}>]> $! \) |
|||
-> \(<[<{drink: <='tea'>, nationality: <='Dane'>}>]> $! \) |
|||
-> \(def current: $; |
|||
$permutationsOf5... -> $pets($) -> \[i]({$current($i), pet: $}! \)! |
|||
\) |
|||
-> \(<[<{nationality: <='Swede'>, pet: <='dog'>}>]> $! \) |
|||
-> \(def current: $; |
|||
$permutationsOf5... -> $smokes($) -> \[i]({$current($i), smoke: $}! \)! |
|||
\) |
|||
-> \(<[<{smoke: <='Pall Mall'>, pet: <='birds'>}>]> $! \) |
|||
-> \(<[<{smoke: <='Dunhill'>, colour: <='yellow'>}>]> $! \) |
|||
-> \(<[(<{smoke: <='Blend'>}>:<{pet: <='cats'>}>)] | [(<{pet: <='cats'>}>:<{smoke: <='Blend'>}>)]> $! \) |
|||
-> \(<[(<{smoke: <='Dunhill'>}>:<{pet: <='horse'>}>)] | [(<{pet: <='horse'>}>:<{smoke: <='Dunhill'>}>)]> $! \) |
|||
-> \(<[<{smoke: <='Blue Master'>, drink: <='beer'>}>]> $! \) |
|||
-> \(<[<{smoke: <='Prince'>, nationality: <='German'>}>]> $! \) |
|||
-> \(<[(<{smoke: <='Blend'>}>:<{drink: <='water'>}>)] | [(<{drink: <='water'>}>:<{smoke: <='Blend'>}>)]> $! \) |
|||
-> \[i](when <{pet: <='zebra'>}> do 'The $.nationality; owns the zebra.$#10;' -> !OUT::write $! |
|||
otherwise $! \) |
|||
-> \[i]('$i;: $;'! \) -> '$... -> '$;$#10;';$#10;' |
|||
-> !OUT::write |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
The German owns the zebra. |
|||
1: {colour=yellow, drink=water, nationality=Norwegian, pet=cats, smoke=Dunhill} |
|||
2: {colour=blue, drink=tea, nationality=Dane, pet=horse, smoke=Blend} |
|||
3: {colour=red, drink=milk, nationality=Englishman, pet=birds, smoke=Pall Mall} |
|||
4: {colour=green, drink=coffee, nationality=German, pet=zebra, smoke=Prince} |
|||
5: {colour=white, drink=beer, nationality=Swede, pet=dog, smoke=Blue Master} |
|||
</pre> |
</pre> |
||