Zebra puzzle: Difference between revisions
Content deleted Content added
→{{header|Perl 6}}: a bit more gather/take |
|||
Line 4,887:
my @facts = (
{ :nat<
{ :nat<
{ :nat<
{ :color<green>, :
{ :drink<coffee>, :color<green> }, # 6 They drink coffee in the green house.
{ :smoke<
{ :color<yellow>, :smoke<
{ :num(3), :drink<milk> }, # 9 In the middle house they drink milk.
{ :num(1), :nat<
{ :smoke<
{ :pet<horse>, :
{ :smoke<
{ :nat<
{ :nat<
{ :drink<water>, :
{ :pet<zebra> }, # who owns this?
);
Line 4,911:
say .pairs.sort.map(*.value.fmt("%-9s")).join(' | ')
for .list;
last; # stop after first solution
}
}
#|
multi sub solve(@houses, @facts [ ]) {
take @houses;
Line 4,922 ⟶ 4,923:
multi sub solve(@houses, @facts is copy) {
my $fact = @facts.shift;
solve(@$_, @facts)
}
}
#| find all possible solutions for pairs of houses with
#| %a attributes, left of a house with %b attributes
multi sub
my @scenarios;
for @houses {
Line 4,935 ⟶ 4,938:
@scenario[$idx-1] = %( %(@houses[$idx-1]), %a );
@scenario[$idx] = %( %(@houses[$idx]), %b );
}
}
}
#| find all possible pairs of houses with %a attributes, either side
#! of a house with %b attributes
multi sub
speculate(@houses, |%b, :Left-Of(%a) );
}
#| find all possible houses that match the given attributes
multi sub
for @houses.grep({plausible($_, %fact)})
my @scenario = @houses.clone;
my $idx = $house<num> - 1;
@scenario[$idx] = %( %$house, %fact );
take @scenario;
}
}
#|
sub plausible(%house, %atts) {
}
</lang>
Line 4,965 ⟶ 4,968:
<pre>
COLOR | DRINK | NAT | NUM | PET | SMOKE
yellow | water |
blue | tea |
red | milk |
green | coffee |
white | beer |
</pre>
|