This commit is contained in:
Yanick Champoux 2023-12-02 13:32:51 -05:00
parent 4c98ac6a61
commit c1984999cf
3 changed files with 16 additions and 13 deletions

View File

@ -4,7 +4,7 @@ package Part1;
use List::AllUtils qw/ max sum /; use List::AllUtils qw/ max sum /;
sub parse_line($line) { sub parse_line ($line) {
my %data; my %data;
$line =~ /Game (\d+):/; $line =~ /Game (\d+):/;
$data{game} = $1; $data{game} = $1;
@ -18,7 +18,7 @@ sub solution_1 ($input) {
sum sum
map { $_->{game} } map { $_->{game} }
grep { $_->{red} <= 12 and $_->{blue} <= 14 and $_->{green} <= 13 } grep { $_->{red} <= 12 and $_->{blue} <= 14 and $_->{green} <= 13 }
map { parse_line($_)} map { parse_line($_) }
split "\n", $input; split "\n", $input;
} }

View File

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

View File

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