Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use v5.16;
- use warnings;
- use List::AllUtils qw(sum zip_by);
- sub matches {
- my $hand = shift;
- # Change cards to hexdigits... Jack to 0, as wild is low
- $hand =~ y#AKQJT#EDC0A#;
- # Collect groups with hash
- my %cards = map { $_ => 0 } (0 .. 9, 'A' .. 'E');
- $cards{$_}++ foreach(split( '', $hand ));
- # Sort by group size (ignoring Jacks), add wild Jacks to best
- my @str = sort { $cards{$b} <=> $cards{$a} } (2 .. 9, 'A' .. 'E');
- $cards{$str[0]} += $cards{0};
- return ($hand, join('', map {$cards{$_}} @str));
- }
- # Parse into array of [hand (hexified), match signature, bid]
- my @hands = map { m#(\w+) (\d+)#; [&matches($1),$2] } <>;
- @hands = sort { $a->[1] <=> $b->[1] or hex($a->[0]) <=> hex($b->[0]) } @hands;
- # Winnings is rank (low to high) * bid
- say "Part 2: ", sum zip_by {$_[0] * $_[1][2]} [1 .. @hands], \@hands;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement