part 1 done
This commit is contained in:
parent
7b44d73c50
commit
3363c98943
@ -2,7 +2,7 @@ use 5.38.0;
|
|||||||
|
|
||||||
package Part1;
|
package Part1;
|
||||||
|
|
||||||
use List::AllUtils qw/ /;
|
use List::AllUtils qw/ max sum /;
|
||||||
|
|
||||||
sub parse_line($line) {
|
sub parse_line($line) {
|
||||||
my %data;
|
my %data;
|
||||||
@ -18,8 +18,21 @@ sub parse_line($line) {
|
|||||||
return \%data;
|
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) {
|
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;
|
1;
|
||||||
|
@ -12,7 +12,16 @@ is Part1::parse_line("Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green") =
|
|||||||
{ green => 2 },
|
{ green => 2 },
|
||||||
]};
|
]};
|
||||||
|
|
||||||
|
is Part1::solution_1( <<'END' ) => 8;
|
||||||
|
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
|
||||||
|
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
|
||||||
|
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
|
||||||
|
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
|
||||||
|
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
|
||||||
|
END
|
||||||
|
|
||||||
my $input = path('input')->slurp;
|
my $input = path('input')->slurp;
|
||||||
|
|
||||||
is Part1::solution_1($input) => 'TODO';
|
is Part1::solution_1($input) => 1853;
|
||||||
|
|
||||||
|
done_testing();
|
||||||
|
1
2023/02/solutions.yml
Normal file
1
2023/02/solutions.yml
Normal file
@ -0,0 +1 @@
|
|||||||
|
1: 1853
|
Loading…
Reference in New Issue
Block a user