pretty
This commit is contained in:
parent
482ae97ee6
commit
046cb8f538
1
2023/.gitignore
vendored
Normal file
1
2023/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
*.bak
|
@ -5,7 +5,7 @@ package Part1;
|
||||
use List::AllUtils qw/ pairmap sum /;
|
||||
use List::UtilsBy qw/ sort_by /;
|
||||
|
||||
sub rank_hand(@hand) {
|
||||
sub rank_hand (@hand) {
|
||||
@hand = split '', shift @hand if @hand == 1;
|
||||
|
||||
my %group;
|
||||
@ -28,7 +28,7 @@ sub rank_hand(@hand) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub handify($str) {
|
||||
sub handify ($str) {
|
||||
state %map = (
|
||||
'T' => 10,
|
||||
'J' => 11,
|
||||
@ -37,26 +37,29 @@ 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;
|
||||
}
|
||||
|
||||
sub parse_input($input) {
|
||||
sub parse_input ($input) {
|
||||
my @lines = split "\n", $input;
|
||||
my @hand_score = pairmap {
|
||||
[ handify($a), $b]
|
||||
} map { split " " } @lines;
|
||||
[ handify($a), $b ]
|
||||
}
|
||||
map { split " " } @lines;
|
||||
return @hand_score;
|
||||
}
|
||||
|
||||
sub score(@hand) {
|
||||
sub score (@hand) {
|
||||
join '', rank_hand(@hand), @hand;
|
||||
}
|
||||
|
||||
sub solution_1 ($input) {
|
||||
my @hand_score = sort_by { score($_->[0]->@*) } parse_input($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;
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ use Part1;
|
||||
use List::AllUtils qw/ pairmap sum max /;
|
||||
use List::UtilsBy qw/ sort_by /;
|
||||
|
||||
sub rank_hand(@hand) {
|
||||
sub rank_hand (@hand) {
|
||||
@hand = split '', shift @hand if @hand == 1;
|
||||
|
||||
my %group;
|
||||
@ -17,11 +17,11 @@ sub rank_hand(@hand) {
|
||||
|
||||
return 7 if 1 >= keys %group;
|
||||
|
||||
return 6 if grep { $_+$jokers >= 4 } values %group;
|
||||
return 6 if grep { $_ + $jokers >= 4 } values %group;
|
||||
|
||||
my $max = max values %group;
|
||||
|
||||
if( $max + $jokers >= 3 ) {
|
||||
if ( $max + $jokers >= 3 ) {
|
||||
return 5 if keys %group <= 2;
|
||||
return 4;
|
||||
}
|
||||
@ -33,7 +33,7 @@ sub rank_hand(@hand) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub handify($str) {
|
||||
sub handify ($str) {
|
||||
state %map = (
|
||||
'T' => 10,
|
||||
'J' => 1,
|
||||
@ -42,25 +42,27 @@ 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;
|
||||
}
|
||||
|
||||
sub parse_input($input) {
|
||||
sub parse_input ($input) {
|
||||
my @lines = split "\n", $input;
|
||||
my @hand_score = pairmap {
|
||||
[ handify($a), $b]
|
||||
} map { split " " } @lines;
|
||||
[ handify($a), $b ]
|
||||
}
|
||||
map { split " " } @lines;
|
||||
return @hand_score;
|
||||
}
|
||||
|
||||
sub score(@hand) {
|
||||
sub score (@hand) {
|
||||
join '', rank_hand(@hand), @hand;
|
||||
}
|
||||
|
||||
sub solution_2 ($input) {
|
||||
my @hand_score = sort_by { score($_->[0]->@*) } parse_input($input);
|
||||
my @hand_score = sort_by { score( $_->[0]->@* ) } parse_input($input);
|
||||
my $rank = 0;
|
||||
return sum map { ++$rank * $_->[1] } @hand_score;
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ for my $part (@parts) {
|
||||
language => 'perl',
|
||||
part => $part->{part},
|
||||
time => $res->cpu_a / $res->iters,
|
||||
persec => $res->iters / $res->cpu_a ,
|
||||
persec => $res->iters / $res->cpu_a,
|
||||
timestamp => DateTime->now->iso8601,
|
||||
};
|
||||
say to_json $result;
|
||||
|
@ -9,17 +9,15 @@ use Part1;
|
||||
my $input = path('input')->slurp;
|
||||
my $example = path('example')->slurp;
|
||||
|
||||
is Part1::rank_hand( 'AAAAA') => 7;
|
||||
is Part1::rank_hand( 'AA8AA') => 6;
|
||||
is Part1::rank_hand( '23332') => 5;
|
||||
is Part1::rank_hand( 'TTT98') => 4;
|
||||
is Part1::rank_hand( '23432') => 3;
|
||||
is Part1::rank_hand( 'A23A4') => 2;
|
||||
is Part1::rank_hand( '23456') => 1;
|
||||
is Part1::rank_hand('AAAAA') => 7;
|
||||
is Part1::rank_hand('AA8AA') => 6;
|
||||
is Part1::rank_hand('23332') => 5;
|
||||
is Part1::rank_hand('TTT98') => 4;
|
||||
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';
|
||||
|
Loading…
Reference in New Issue
Block a user