version 2

This commit is contained in:
Yanick Champoux 2023-12-02 13:30:04 -05:00
parent 57b7b318b4
commit 4c98ac6a61
6 changed files with 9 additions and 22 deletions

View File

@ -6,31 +6,18 @@ use List::AllUtils qw/ max sum /;
sub parse_line($line) {
my %data;
$line =~ s/Game (\d+)://;
$line =~ /Game (\d+):/;
$data{game} = $1;
my @entries = split ';', $line;
$data{entries} = [
map { +{ map { /(\d+) (\w+)/; $2 => $1 } split ",", $_ } } @entries
];
$data{$_} = max $line =~ /(\d+) $_/g for qw/ red green blue /;
return \%data;
}
sub aggregate_entries($game) {
my %agg = map {
my $color = $_;
$color => max map { $_->{$color} // 0 } $game->{entries}->@*
} qw/ red green blue /;
+{ %$game, %agg};
}
sub solution_1 ($input) {
sum
map { $_->{game} }
grep { $_->{red} <= 12 and $_->{blue} <= 14 and $_->{green} <= 13 }
map { aggregate_entries($_) }
map { parse_line($_)}
split "\n", $input;
}

View File

@ -9,7 +9,6 @@ use List::AllUtils qw/ min product sum /;
sub solution_2 ($input) {
sum
map { product $_->@{qw/ red blue green /}}
map { Part1::aggregate_entries($_) }
map { Part1::parse_line($_)}
split "\n", $input;
}

View File

@ -33,7 +33,7 @@ for my $part (@parts) {
day => $day,
year => $year,
#variant => '',
variant => 'v2',
language => 'perl',
part => $part->{part},
time => $res->cpu_a / $res->iters,

View File

@ -7,10 +7,9 @@ use Path::Tiny;
use Part1;
is Part1::parse_line("Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green") => +{ game => 1, entries => [
{ blue => 3, red => 4 }, { red => 1, green => 2, blue => 6},
{ green => 2 },
]};
is Part1::parse_line("Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green") => +{ game => 1,
blue => 6, red => 4, green => 2
};
is Part1::solution_1( <<'END' ) => 8;
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green

View File

@ -17,4 +17,4 @@ END
my $input = path('input')->slurp;
cmp_ok Part2::solution_2($input), ">", 1720;
is Part2::solution_2($input) => 'TODO';
is Part2::solution_2($input) => 72706;

View File

@ -4,3 +4,5 @@
{"day":1,"year":2023,"language":"javascript","part":"2","timestamp":"2023-12-01T20:55:40.989Z","time":0.004363576172638605,"persec":229.1698277826353}
{"language":"perl","timestamp":"2023-12-02T16:40:42","part":1,"day":"2","persec":297.067171239357,"year":"2023","time":0.00336624203821656}
{"language":"perl","timestamp":"2023-12-02T16:40:55","part":2,"day":"2","time":0.00338658146964856,"year":"2023","persec":295.283018867925}
{"timestamp":"2023-12-02T18:29:20","day":"2","part":1,"variant":"v2","year":"2023","time":0.00388473053892216,"language":"perl","parsec":257.418111753372}
{"day":"2","timestamp":"2023-12-02T18:29:32","part":2,"year":"2023","variant":"v2","parsec":258.117195004803,"language":"perl","time":0.00387420915519166}