commit 05549305f7a9aeb223b891c91c981302b1cbbbe9
parent 54e078e44e302e7bb0cb6dc0f406d22cd5a76cb3
Author: Samir Parikh <noreply@samirparikh.com>
Date: Fri, 13 Jan 2023 18:56:06 +0000
cleanup code
Diffstat:
1 file changed, 6 insertions(+), 31 deletions(-)
diff --git a/day12/Day12.pm b/day12/Day12.pm
@@ -14,7 +14,7 @@ use Data::Dumper;
use Log::Log4perl ();
use Log::Log4perl::Level ();
Log::Log4perl->easy_init(
- Log::Log4perl::Level::to_priority( 'TRACE' )
+ Log::Log4perl::Level::to_priority( 'OFF' )
); # set to 'OFF', 'INFO', 'DEBUG' or 'TRACE' for successively more information
my $logger = Log::Log4perl->get_logger();
@@ -35,11 +35,6 @@ sub define_graph ( $matrix_ref )
my @mat = @{ $matrix_ref };
my %neighbors;
my %elevation;
-# my $start_row = '';
-# my $start_col = '';
-# my $end_row = '';
-# my $end_col = '';
-# my ( $start_row, $start_col, $end_row, $end_col ) = ( -1, -1, -1, -1 );
my $r = scalar @mat;
my $c = scalar @{ $mat[ 0 ] };
foreach my $row ( 0 .. $r - 2 )
@@ -52,20 +47,14 @@ sub define_graph ( $matrix_ref )
if ( $value == ord( 'S' ) )
{
-# $key = "start";
$value = ord( 'a' );
push @{ $neighbors{ "start" } } => $key;
-# $start_row = $row;
-# $start_col = $col;
}
if ( $value == ord( 'E' ) )
{
-# $key = "end";
$value = ord( 'z' );
push @{ $neighbors{ $key } } => "end";
-# $end_row = $row;
-# $end_col = $col;
}
$elevation{ $key } = $value;
@@ -75,32 +64,20 @@ sub define_graph ( $matrix_ref )
my $n_row = $row + $neighbor->[0];
my $n_col = $col + $neighbor->[1];
+ # it's possible we have to add a neighbor which is the end square
+ # but whose elevation needs to be set to ord( 'z' ), which is 122,
+ # rather than ord( 'E' ), which is 69.
my $neighbor_elevation = ( $mat[ $n_row ][ $n_col ] == ord( 'E' ) ) ?
ord( 'z' ) :
$mat[ $n_row ][ $n_col ];
+ # we don't want to update the value of $mat[ $n_row ][ $n_col ] just
+ # yet because we have to process the end node above
push @{ $neighbors{ $key } } => "$n_row:$n_col"
- #unless ( $mat[ $n_row ][ $n_col ] > $value + 1 );
unless ( $neighbor_elevation > $value + 1 );
-# if ( $mat[ $n_row ][ $n_col ] < $value + 1 )
-# {
-# if ( $n_row == $start_row && $n_col == $start_col )
-# {
-# push @{ $neighbors{ $key } } => "start";
-# }
-# elsif ( $n_row == $end_row && $n_col == $end_col )
-# {
-# push @{ $neighbors{ $key } } => "end";
-# }
-# else
-# {
-# push @{ $neighbors{ $key } } => "$n_row - $n_col";
-# }
-# }
}
}
}
- say Dumper \%neighbors;
return ( \%neighbors, \%elevation );
}
@@ -125,7 +102,6 @@ sub find_path ( $n, $e )
$paths{ $search } = [ ];
# array that stores list of squares to be searched
- #my @queue = @{ $neighbors{ $search } };
my @queue = ( $search );
while ( @queue )
@@ -154,7 +130,6 @@ sub find_path ( $n, $e )
unless ( $searched{ $neighbor } )
{
$logger->trace( "adding $square to $neighbor path" );
- #push @{ $paths{ $neighbor } } => @{ $paths{ $square } }, $square;
@{ $paths{ $neighbor } } = ( @{ $paths{ $square } }, $square );
$logger->trace( "path to $neighbor is now ",
join "->", @{ $paths{ $neighbor } } );