2023-12-01 18:03:18 +00:00
|
|
|
use 5.38.0;
|
|
|
|
|
|
|
|
package Part1;
|
|
|
|
|
2023-12-02 16:28:35 +00:00
|
|
|
use List::AllUtils qw/ max sum /;
|
2023-12-01 18:03:18 +00:00
|
|
|
|
2023-12-02 16:17:13 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-12-02 16:28:35 +00:00
|
|
|
sub aggregate_entries($game) {
|
|
|
|
my %agg = map {
|
|
|
|
my $color = $_;
|
|
|
|
$color => max map { $_->{$color} // 0 } $game->{entries}->@*
|
|
|
|
} qw/ red green blue /;
|
|
|
|
+{ %$game, %agg};
|
|
|
|
}
|
|
|
|
|
2023-12-01 18:03:18 +00:00
|
|
|
sub solution_1 ($input) {
|
2023-12-02 16:28:35 +00:00
|
|
|
sum
|
|
|
|
map { $_->{game} }
|
|
|
|
grep { $_->{red} <= 12 and $_->{blue} <= 14 and $_->{green} <= 13 }
|
|
|
|
map { aggregate_entries($_) }
|
|
|
|
map { parse_line($_)}
|
|
|
|
split "\n", $input;
|
2023-12-01 18:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|