adventofcode/2024/11/Part1.pm

50 lines
955 B
Perl
Raw Normal View History

2024-12-11 18:40:40 +00:00
use lib '../../perl-lib';
use 5.36.0;
package Part1;
use List::AllUtils qw/ sum /;
use Moo;
extends 'AoC::Puzzle';
has '+input' => default => sub ($self) {
[ split ' ', $self->path_file->slurp ];
};
has nbr_iterations => is => 'ro', default => 25;
sub evolve ( $self, $stone ) {
return (1) if $stone == 0;
return ( $stone * 2024 ) if length($stone) % 2;
my $x = $stone % 10**( length($stone) / 2 );
my $y = int( $stone / 10**( length($stone) / 2 ) );
return ( $y, $x );
}
sub print_line ( $self, @stones ) {
say join " ", @stones;
}
sub solve ($self) {
my @stones = $self->input->@*;
my %stones;
$stones{$_}++ for @stones;
for ( 1 .. $self->nbr_iterations ) {
my %new_stones;
for my $stone ( keys %stones ) {
$new_stones{$_} += $stones{$stone} for $self->evolve($stone);
}
%stones = %new_stones;
}
return sum values %stones;
}
1;