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
- $hand =~ y#AKQJT#EDCBA#;
- # Collect groups with hash
- my %cards = map { $_ => 0 } (2 .. 9, 'A' .. 'E');
- $cards{$_}++ foreach(split( '', $hand ));
- # Sort by group size
- my @str = sort { $cards{$b} <=> $cards{$a} } keys %cards;
- 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 1: ", sum zip_by {$_[0] * $_[1][2]} [1 .. @hands], \@hands;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement