adventofcode/2024/10/Part1.pm

59 lines
1.1 KiB
Perl
Raw Permalink Normal View History

2024-12-10 23:34:16 +00:00
use lib qw/ . /;
use lib qw! ../../perl-lib !;
use 5.36.0;
2024-12-10 23:39:19 +00:00
use AoC::Puzzle;
2024-12-10 23:34:16 +00:00
package Part1;
use Moo;
use AoC::Grid;
use List::AllUtils qw/ sum /;
2024-12-10 23:39:19 +00:00
extends 'AoC::Puzzle';
2024-12-10 23:34:16 +00:00
has '+input', default => sub ($self) {
AoC::Grid->new( string => $self->file_slurp );
};
sub trailhead_score ( $self, $x, $y ) {
my $grid = $self->input;
my %endpoints;
my @points = ( [ $x, $y, 0 ] );
while ( my $p = shift @points ) {
if ( $p->[2] == 9 ) {
$endpoints{ join ':', @$p }++;
next;
}
my $v = $p->[2] + 1;
push @points,
map { [ @$_, $v ] }
grep { $grid->get(@$_) == $v }
$grid->ortho_neighbors( $p->@[ 0, 1 ] );
}
return $self->trailhead_score_resolution(%endpoints);
}
sub trailhead_score_resolution ( $self, %endpoints ) {
return scalar keys %endpoints;
}
sub solve ($self) {
my @points;
$self->input->foreach(
sub ( $x, $y, $v ) {
push @points, [ $x, $y ] if $v == 0;
}
);
return sum map { $self->trailhead_score(@$_) } @points;
}
1;