This commit is contained in:
Yanick Champoux 2023-12-07 11:30:37 -05:00
parent 046cb8f538
commit cbc7a283a3

View File

@ -11,15 +11,14 @@ sub rank_hand (@hand) {
my %group; my %group;
$group{$_}++ for @hand; $group{$_}++ for @hand;
#use DDP; p %group;
return 7 if 1 == keys %group; return 7 if 1 == keys %group;
return 6 if grep { $_ == 4 } values %group; return 6 if grep { $_ == 4 } values %group;
return 5 if keys %group == 2 and grep { $_ == 3 } values %group; if( grep { $_ == 3 } values %group ) {
return 5 if keys %group == 2;
return 4 if grep { $_ == 3 } values %group; return 4;
}
return 3 if keys %group == 3; return 3 if keys %group == 3;
@ -37,19 +36,16 @@ sub handify ($str) {
A => 14 A => 14
); );
my @cards = return [
map { chr( ord('a') + $_ - 2 ) } map { $map{$_} // $_ } split '', $str; map { chr( ord('a') + $_ - 2 ) } map { $map{$_} // $_ } split '', $str
];
return \@cards;
} }
sub parse_input ($input) { sub parse_input ($input) {
my @lines = split "\n", $input; return pairmap {
my @hand_score = pairmap {
[ handify($a), $b ] [ handify($a), $b ]
} }
map { split " " } @lines; map { split " " } split "\n", $input;
return @hand_score;
} }
sub score (@hand) { sub score (@hand) {
@ -57,11 +53,8 @@ sub score (@hand) {
} }
sub solution_1 ($input) { sub solution_1 ($input) {
my @hand_score = sort_by { score( $_->[0]->@* ) } parse_input($input);
my $rank = 0; my $rank = 0;
return sum map { ++$rank * $_->[1] } sort_by { score( $_->[0]->@* ) } parse_input($input);
# use DDP; p @hand_score;
return sum map { ++$rank * $_->[1] } @hand_score;
} }
1; 1;