This commit is contained in:
Yanick Champoux 2023-12-07 11:23:53 -05:00
parent 482ae97ee6
commit 046cb8f538
6 changed files with 48 additions and 44 deletions

1
2023/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.bak

View File

@ -37,7 +37,8 @@ sub handify($str) {
A => 14
);
my @cards = map { chr( ord('a') + $_ - 2 )} map { $map{$_} // $_ } split '', $str;
my @cards =
map { chr( ord('a') + $_ - 2 ) } map { $map{$_} // $_ } split '', $str;
return \@cards;
}
@ -46,7 +47,8 @@ sub parse_input($input) {
my @lines = split "\n", $input;
my @hand_score = pairmap {
[ handify($a), $b ]
} map { split " " } @lines;
}
map { split " " } @lines;
return @hand_score;
}
@ -57,6 +59,7 @@ sub score(@hand) {
sub solution_1 ($input) {
my @hand_score = sort_by { score( $_->[0]->@* ) } parse_input($input);
my $rank = 0;
# use DDP; p @hand_score;
return sum map { ++$rank * $_->[1] } @hand_score;
}

View File

@ -42,7 +42,8 @@ sub handify($str) {
A => 14
);
my @cards = map { chr( ord('a') + $_ - 2 )} map { $map{$_} // $_ } split '', $str;
my @cards =
map { chr( ord('a') + $_ - 2 ) } map { $map{$_} // $_ } split '', $str;
return \@cards;
}
@ -51,7 +52,8 @@ sub parse_input($input) {
my @lines = split "\n", $input;
my @hand_score = pairmap {
[ handify($a), $b ]
} map { split " " } @lines;
}
map { split " " } @lines;
return @hand_score;
}

View File

@ -17,9 +17,7 @@ is Part1::rank_hand( '23432') => 3;
is Part1::rank_hand('A23A4') => 2;
is Part1::rank_hand('23456') => 1;
is [ Part1::parse_input( '2345A 123') ] => [
[ [qw/ a b c d m /], 123 ]
];
is [ Part1::parse_input('2345A 123') ] => [ [ [qw/ a b c d m /], 123 ] ];
is Part1::solution_1($example) => 6440;
is Part1::solution_1($input) => 'TODO';