commit 3a2d976a412563ae5209483d42622ed0f25b7fcd
parent f43ed2a46a7615e3c51d506c83c2b7bfda921630
Author: Samir Parikh <noreply@samirparikh.com>
Date: Fri, 13 Jan 2023 12:57:35 +0000
work in progress
Diffstat:
2 files changed, 32 insertions(+), 47 deletions(-)
diff --git a/day12/Day12.pm b/day12/Day12.pm
@@ -23,38 +23,16 @@ sub define_matrix ( $input )
return $matrix;
}
-sub init_graph_costs {
- my $matrix_ref = shift;
- my @mat = @{ $matrix_ref };
- my %grph;
-# my %csts;
- my $r = scalar @mat;
- my $c = scalar @{ $mat[0] };
- foreach my $row (0 .. $r - 2) {
- foreach my $col (0 .. $c - 2) {
-# $csts{$row, '-', $col} = 'inf';
- foreach my $neighbor ([0, 1], [1, 0], [0, -1], [-1, 0]) {
- my $n_row = $row + $neighbor->[0];
- my $n_col = $col + $neighbor->[1];
- $grph{$row, '-', $col}{$n_row, '-', $n_col} = $mat[$n_row][$n_col]
- unless ($mat[$n_row][$n_col] == $LIMIT);
- }
- }
- }
- $grph{'start'}{0, '-', 0} = $mat[0][0]; # start node to 0, 0
- $grph{$r - 2, '-', $c - 2}{'end'} = 0; # n, n node to end
-# $csts{0, '-', 0} = $mat[0][0];
-# $csts{'end'} = 'inf';
- say Dumper \%grph;
-# return (\%grph, \%csts);
- return ( \%grph );
-}
-
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 )
@@ -69,12 +47,18 @@ sub define_graph ( $matrix_ref )
{
$key = "start";
$value = ord( 'a' );
+ $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;
@@ -83,15 +67,27 @@ sub define_graph ( $matrix_ref )
{
my $n_row = $row + $neighbor->[0];
my $n_col = $col + $neighbor->[1];
-# $grph{$row, '-', $col}{$n_row, '-', $n_col} = $mat[$n_row][$n_col]
-# unless ($mat[$n_row][$n_col] == $LIMIT);
-# push @{ $neighbors{ $key } } => [ $n_row, $n_col ]
push @{ $neighbors{ $key } } => "$n_row - $n_col"
unless ( $mat[ $n_row ][ $n_col ] > $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;
+ say Dumper \%neighbors;
return ( \%neighbors, \%elevation );
}
@@ -116,16 +112,13 @@ sub find_path ( $n, $e )
$paths{ $search } = [ ];
# array that stores list of squares to be searched
- my @queue = @{ $neighbors{ $search } };
-# foreach (@queue)
-# {
-# say "@{ $_ }";
-# }
- while ( @queue )
+ #my @queue = @{ $neighbors{ $search } };
+ my @queue = ( $search );
+
+while ( @queue )
{
say "queue is @queue";
my $square = shift @queue;
-# my $key = "$square->[0] - $square->[1]";
say "checking square at $square";
# unless we've already searched this square
unless ( $searched{ $square } )
diff --git a/day12/day12.pl b/day12/day12.pl
@@ -10,14 +10,6 @@ use Day12 qw( define_matrix define_graph find_path );
@ARGV = "input" unless @ARGV;
chomp( my $input = do { local $/; <> } );
-#say $input;
my $matrix = define_matrix( $input );
-#foreach ( @{ $matrix } )
-#{
-# say " @{$_} ";
-#}
-
-#my ($graph, $costs) = init_graph_costs( $matrix );
-#my $graph = init_graph_costs($matrix);
my ( $neighbors, $elevation ) = define_graph( $matrix );
-find_path( $neighbors, $elevation );
+#find_path( $neighbors, $elevation );