All,
In my never ending quest to improve my Perl6 skills as well as spread advocacy, I have written the following code to solve thor's problem of proving through brute force that all hands of cribbage with a 5 are worth at least 2 points.
use v6; my @deck = new_deck(); my $next = combo(5, @deck); my @combo; while ( @combo = $next() ) { # Skip all hands that do not contain a 5 next if ! grep { $_<val> == 5 } @combo; # Skip all hands that have a score of at least 2 my $score = score( @combo ); next if $score > 1; # Print out the rest say join ' ', $score, map -> $_ { $_<disp> ~ $_<suit> } @combo; } sub score ( @hand ) returns Int { my $score = 0; # Last card is always cut card (for purposes of right-jack and flu +sh) # [234] of a kind my %of_a_kind = (1 => 0, 2 => 2, 3 => 6, 4 => 12); my %seen; for ( map -> $_ { $_<disp> } @hand ) { %seen{$_}++; } for ( values %seen ) { $score += %of_a_kind{$_} } # Flush my %suit = map -> $_ { $_<suit> => 1 } @hand[0 .. 3]; $score += 4 if +%suit == 1; $score++ if %suit{ @hand[ -1 ] }; # Count 15's my @vals = map -> $_ { $_<val> } @hand; for ( 2 .. 5 ) { my $next = combo($_, @vals); my @combo; while ( @combo = $next() ) { my $tot = 0; for ( @combo ) { $tot += $_ } $score += 2 if $tot == 15; } } # Check for right-jack for ( @hand[0 .. 3] ) { if ( $_<disp> eq 'J' ) { if ( $_<suit> eq @hand[ -1 ]<suit> ) { $score++; last; } } } # Check for runs my @nums = map -> $_ { $_<num> } @hand; my $found; for ( 5, 4, 3 ) { last if $found; my $next = combo($_, @nums); my @combo; while ( @combo = $next() ) { @combo = sort { $^a <=> $^b } @combo; my ($prev, $in_sequence) = (0, 1); for ( @combo ) { if ( ! $prev ) { $prev = $_; next; } if ( $_ - $prev != 1 ) { $in_sequence = 0; last; } $prev = $_; } if ( $in_sequence ) { $found = 1; $score += $_; } } } return $score; } sub combo (Int $by is copy, @list is copy) returns Ref { my @position = 0 .. ($by - 2), $by - 2; my @stop = @list.elems - $by .. @list.end; my $done = undef; return sub { return () if $done; my $cur = @position.end; while ( ++@position[ $cur ] > @stop[ $cur ] ) { --$cur; @position[ $cur ]++; next if @position[ $cur ] > @stop[ $cur ]; my $new_pos = @position[ $cur ]; @position[ $cur .. @position.end ] = $new_pos .. ($new_pos + + $by); last; } $done = 1 if @position[ 0 ] == @stop[ 0 ]; return @list[ @position ]; }; } sub new_deck () returns Array { my @deck; my %val = map -> $_ { $_ => $_ } 1..10; %val{ 11..13 } = (10) xx 3; my %disp = map -> $_ { $_ => $_ } 2..10; %disp{ 1, 11..13 } = qw<A J Q K>; for ( 1..13 ) { push @deck, { 'num' => $_, 'val' => %val{$_}, 'suit' => 'H', 'disp' => + %disp{$_} }, { 'num' => $_, 'val' => %val{$_}, 'suit' => 'D', 'disp' => + %disp{$_} }, { 'num' => $_, 'val' => %val{$_}, 'suit' => 'C', 'disp' => + %disp{$_} }, { 'num' => $_, 'val' => %val{$_}, 'suit' => 'S', 'disp' => + %disp{$_} }, } return @deck; }

Your challenge is to refactor or rewrite the code taking advantage of as many features of Perl6 as you can. Obfuscation is frowned on while clear succinct code using a new p6ism is smiled upon. I would encourage everyone to try their solutions using the latest Pugs and if something doesn't work - write a test! A secondary goal is to improve efficiency though it is a must to consider all hands.

If anyone feels that learning isn't reward enough and is interested in a prize, let me know and I will consider one. Any award beyond bragging rights will require a more strict set of rules though. Happy Hacking!

Cheers - L~R

Note: I had to work around a few pugsbugs but hopefully they will be fixed soon making the code better.

Update: The latest and greatest version can now be found here.


In reply to Perl6 Contest: Test your Skills by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.