Anonymous user
Set puzzle: Difference between revisions
reorder Perl vs Perl 6
(→{{header|J}}: sort output) |
(reorder Perl vs Perl 6) |
||
Line 1,074:
purple, oval, one, open</pre>
=={{header|Perl}}==
Line 1,245 ⟶ 1,185:
purple three squiggle striped
red one oval striped</pre>
=={{header|Perl 6}}==
{{works with|rakudo|2013-02-11}}
This uses the <tt>combine</tt> routine from [[Combinations#Perl_6]] task. The trick here is to allocate three different bits for each enum, with the result that the cards of a matching set OR together to produce a 4-digit octal number that contains only the digits 1, 2, 4, or 7. This OR is done by funny looking <tt>[+|]</tt>, which is the reduction form of <tt>+|</tt>, which is the numeric bitwise OR. (Because Perl 6 stole the bare <tt>|</tt> operator for composing junctions instead.)
<lang perl6>enum Color (red => 0o1000, green => 0o2000, purple => 0o4000);
enum Count (one => 0o100, two => 0o200, three => 0o400);
enum Shape (oval => 0o10, squiggle => 0o20, diamond => 0o40);
enum Style (solid => 0o1, open => 0o2, striped => 0o4);
my @deck := (Color.enums X Count.enums X Shape.enums X Style.enums).tree;
sub MAIN($DRAW = 9, $GOAL = $DRAW div 2) {
my @combinations = combine(3, [^$DRAW]);
my @draw;
repeat until (my @sets) == $GOAL {
@draw = @deck.pick($DRAW);
my @bits = @draw.map: { [+] @^enums».value }
@sets = gather for @combinations -> @c {
take @draw[@c].item when /^ <[1247]>+ $/ given ( [+|] @bits[@c] ).base(8);
}
}
say "Drew $DRAW cards:";
show-cards @draw;
for @sets.kv -> $i, @cards {
say "\nSet {$i+1}:";
show-cards @cards;
}
}
sub show-cards(@c) { for @c -> $c { printf " %-6s %-5s %-8s %s\n", $c».key } }</lang>
{{out}}
<pre>Drew 9 cards:
red two diamond striped
purple one squiggle solid
purple three squiggle solid
red two squiggle striped
red two oval striped
green one diamond open
red three diamond solid
green three squiggle open
purple two diamond striped
Set 1:
red two diamond striped
red two squiggle striped
red two oval striped
Set 2:
purple one squiggle solid
red two squiggle striped
green three squiggle open
Set 3:
purple three squiggle solid
red two oval striped
green one diamond open
Set 4:
green one diamond open
red three diamond solid
purple two diamond striped</pre>
=={{header|Python}}==
<lang python>#!/usr/bin/python
|