Zebra puzzle: Difference between revisions
Content added Content deleted
No edit summary |
m (→General solver with relational algebra: update to stricter typing) |
||
Line 6,993: | Line 6,993: | ||
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> |
||
// There are five houses. |
|||
// The English man lives in the red house. |
|||
// The Swede has a dog. |
|||
// The Dane drinks tea. |
|||
// The green house is immediately to the left of the white house. |
|||
// They drink coffee in the green house. |
|||
// The man who smokes Pall Mall has birds. |
|||
// In the yellow house they smoke Dunhill. |
|||
// In the middle house they drink milk. |
|||
// The Norwegian lives in the first house. |
|||
// The man who smokes Blend lives in the house next to the house with cats. |
|||
// In a house next to the house where they have a horse, they smoke Dunhill. |
|||
// The man who smokes Blue Master drinks beer. |
|||
// The German smokes Prince. |
|||
// The Norwegian lives next to the blue house. |
|||
// They drink water in a house next to the house where they smoke Blend. |
|||
// |
|||
// The question is, who owns the zebra? |
|||
// |
|||
//Additionally, list the solution for all the houses. |
|||
//Optionally, show the solution is unique. |
|||
⚫ | |||
processor EinsteinSolver |
processor EinsteinSolver |
||
@: [{|{}|}]; // A list of possible relations, start with one relation with an empty tuple |
@: [{|{}|}]; // A list of possible relations, start with one relation with an empty tuple |
||
Line 7,014: | Line 7,038: | ||
operator (a nextTo&{byField:, bMinusA:} b) |
operator (a nextTo&{byField:, bMinusA:} b) |
||
⚫ | |||
@EinsteinSolver: [$@EinsteinSolver... -> \( |
@EinsteinSolver: [$@EinsteinSolver... -> \( |
||
def in: $; |
def in: $; |
||
def temp: {| $... -> {$, ES_temp__: $(byField)::raw} |}; |
def temp: {| $... -> {$, ES_temp__: ($(byField)::raw)"1"} |}; |
||
def numbers: [$temp({ES_temp__:})... -> $.ES_temp__]; |
def numbers: [$temp({ES_temp__:})... -> $.ES_temp__]; |
||
$numbers... -> \( |
$numbers... -> \( |
||
def aNumber: $; |
def aNumber: $; |
||
def bNumbers: [$bMinusA... -> $ + $aNumber]; |
def bNumbers: [$bMinusA... -> ($ + $aNumber)"1"]; |
||
def new: ($temp matching {| {$a, ES_temp__: $aNumber} |}); |
def new: ($temp matching {| {$a, ES_temp__: $aNumber} |}); |
||
@: ($new union (($temp notMatching {| $a |}) notMatching {| {ES_temp__: $aNumber} |})); |
@: ($new union (($temp notMatching {| $a |}) notMatching {| {ES_temp__: $aNumber} |})); |