use lib qw/ . /; use lib qw! ../../perl-lib !; use 5.36.0; use AoC::Puzzle; package Part1; use Moo; use AoC::Grid; use List::AllUtils qw/ sum /; extends 'AoC::Puzzle'; 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;