adventofcode/2023/02/Part1.pm
2023-12-02 11:28:35 -05:00

39 lines
739 B
Perl

use 5.38.0;
package Part1;
use List::AllUtils qw/ max sum /;
sub parse_line($line) {
my %data;
$line =~ s/Game (\d+)://;
$data{game} = $1;
my @entries = split ';', $line;
$data{entries} = [
map { +{ map { /(\d+) (\w+)/; $2 => $1 } split ",", $_ } } @entries
];
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;
}
1;