Set puzzle: Difference between revisions
Content added Content deleted
(→{{header|D}}: removed some of the constness because the code wouldn't compile otherwise) |
(→{{header|Perl 6}}: add entry) |
||
Line 426: | Line 426: | ||
[Card: PURPLE, TWO, SQUIGGLE, SOLID]</pre> |
[Card: PURPLE, TWO, SQUIGGLE, SOLID]</pre> |
||
=={{header|Perl 6}}== |
|||
This uses the <tt>combine</tt> routine from [[Combinations#Perl_6]] task. The trick here is to OR all the bits together to make an octal digit for each enum, with the result that the cards of a matching set produce a 4-digit octal numbers that contains only the digits 1, 2, 4, or 7. |
|||
<lang perl6>enum Color (red => 512, green => 1024, purple => 2048); |
|||
enum Count (one => 64, two => 128, three => 256); |
|||
enum Shape (oval => 8, squiggle => 16, diamond => 32); |
|||
enum Style (solid => 1, open => 2, striped => 4); |
|||
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) { @c.map: -> $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}}== |
=={{header|Python}}== |
||
<lang python>#!/usr/bin/python |
<lang python>#!/usr/bin/python |