commit bc0e4535045530a200ecbcef4bbaacc5d6cf141f
parent 605e6a6de8270c97055e13724de8ef48fa621178
Author: Samir Parikh <noreply@samirparikh.com>
Date: Tue, 21 Dec 2021 16:24:00 +0000
update day09-2.pl but still have an issue
I am "double-counting" neighbors of a lowpoint because I am not
confirming whether it was already examined. Will try to update
the height of an already checked location to '9' to prevent this.
Diffstat:
1 file changed, 18 insertions(+), 2 deletions(-)
diff --git a/day09-2.pl b/day09-2.pl
@@ -13,11 +13,26 @@ sub find_basin {
my $location = shift @queue;
my $r = $location->[0];
my $c = $location->[1];
- my $location_height = $heightmap_ref->[$r][$c]
+ my $location_height = $heightmap_ref->[$r][$c];
+ say "checking location of $r, $c with height of $location_height";
+ # check surrounding locations for increase in height
+ foreach my $offset ([0, -1], [0, 1], [-1, 0], [1, 0]) {
+ my $neighbor_row = $r + $offset->[0];
+ my $neighbor_col = $c + $offset->[1];
+ my $neighbor_height = $heightmap_ref->[$neighbor_row][$neighbor_col];
+ say "neighbor at $neighbor_row, $neighbor_col has height $neighbor_height";
+ next if ($neighbor_height == 9);
+ if ($neighbor_height == $location_height + 1) { # found increase
+ say "found winner";
+ push @queue => [ $neighbor_row, $neighbor_col ]; # add neighbor to queue
+ $size++;
+ }
+ }
}
#say "row is $location->[0] and column is $location->[1]";
#my $location_height = $heightmap_ref->[$location->[0]][$location->[1]];
#say "recvd lowpoint of height $location_height at $row, $column";
+ return $size;
}
my @heightmap = map{ [m/\d/g, 9] } <>; # add extra column of 9s
@@ -41,7 +56,8 @@ foreach my $row (0 .. $rows - 2) { # don't care about last row that we added
if ($lowpoint) {
say "found lowpoint of height $height at $row, $column";
$risk_level += $height + 1;
- find_basin( $row, $column, \@heightmap );
+ my $basin_size = find_basin( $row, $column, \@heightmap );
+ say "basin size is $basin_size";
}
}
}