use strict; use warnings; use utf8; print "P3\n".(13*13)." ".(13*13*13)."\n".(29*2)."\n"; sub do_hand { my @cards = @_; my $score = 0; my $nobs_possible = 0; my $flush_possible = 5; for my $which (0..31) { my @these_cards; for my $i (0..4) { push @these_cards, $cards[$i] if $which & (1<<$i); } if(@these_cards == 1 && $these_cards[0] == 11 && $which != 16) { $nobs_possible = 1; } if(@these_cards == 2 && $these_cards[0] == $these_cards[1]) { $score += 2; # pair if($which & 16) { $flush_possible &&= 4; } else { $flush_possible = 0; } } my $sum = 0; for my $c (@these_cards) { $sum += ($c > 10 ? 10 : $c); } if($sum == 15) { $score += 2; } #if(@these_cards >= 3) { # @these_cards = sort @these_cards; # my $run = 1; # for my $i (0..$#these_cards-1) { # $run = 0 unless $these_cards[$i]+1 == $these_cards[$i+1]; # } # if($run && @these_cards == 3) { # $score += 3; # } elsif($run && @these_cards == 4) { # $score -= 2; # run of 4 was counted as 3+3 # } # # I think run of 5 ends up being correct? #} } @cards = sort {$a <=> $b} @cards; my $run = 1; my $run_mult = 1; for my $i (0..3) { if($cards[$i]+1 == $cards[$i+1]) { $run++; } elsif($cards[$i] == $cards[$i+1]) { $run_mult *= 2; } elsif($run >= 3) { last; } else { $run = 1; $run_mult = 1; } #print "$cards[$i]-$cards[$i+1] $run $run_mult\n"; } $score += $run * $run_mult if $run >= 3; return $score, $score + $nobs_possible + $flush_possible; } sub check_hand { my ($expected, $max_expected, @cards) = @_; my ($score, $max_score) = do_hand(@cards); print STDERR "$score!=$expected $max_score!=$max_expected: @cards\n" unless $score == $expected && $max_score == $max_expected; } check_hand(3, 8, 8, 9, 10, 12, 13); check_hand(3, 9, 7, 9, 10, 11, 13); check_hand(3, 9, 8, 9, 11, 12, 13); check_hand(8, 8, 1, 1, 2, 3, 5); check_hand(8, 9, 1, 10, 10, 11, 12); check_hand(8, 9, 1, 10, 11, 11, 12); check_hand(8, 13, 1, 10, 11, 12, 12); check_hand(8, 8, 13, 8, 8, 9, 10); check_hand(8, 8, 13, 8, 9, 9, 10); check_hand(8, 12, 13, 8, 9, 10, 10); check_hand(12, 12, 13, 7, 7, 8, 9); check_hand(12, 12, 13, 7, 8, 8, 9); check_hand(10, 14, 13, 7, 8, 9, 9); check_hand(4, 10, 1, 10, 11, 12, 13); check_hand(10, 11, 10, 10, 11, 12, 13); check_hand(10, 11, 11, 10, 11, 12, 13); check_hand(10, 11, 12, 10, 11, 12, 13); check_hand(10, 15, 13, 10, 11, 12, 13); check_hand(5, 11, 9, 10, 11, 12, 13); check_hand(28, 29, 5, 5, 5, 11, 5); for $_ (0..13**5-1) { my @cards; for my $i (0..4) { push @cards, (int($_/13**$i) % 13) + 1; } #@cards = sort @cards; if($cards[0] == $cards[1] && $cards[1] == $cards[2] && $cards[2] == $cards[3] && $cards[3] == $cards[4]) { print "58 0 0\n"; next; } my ($score, $max_score) = do_hand(@cards); print +($score*2).' '.($score+$max_score).' '.($max_score*2)."\n"; }