commit 7d9faeae1dfb12f9efc9ceb07e99b14623de790b
parent 5c8e94d2fcf4dddc3ce01d3b7c507e1c1876581d
Author: Samir Parikh <noreply@samirparikh.com>
Date: Sun, 19 Dec 2021 22:20:13 +0000
update with comments
Diffstat:
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/day09-1.pl b/day09-1.pl
@@ -5,20 +5,21 @@ use warnings;
use v5.22;
use Data::Dumper;
-my @heightmap = map{ [m/\d/g, 10] } <>;
+my @heightmap = map{ [m/\d/g, 10] } <>; # add extra column of 10s
my $columns = @{$heightmap[0]};
-push @heightmap => [ (10) x $columns ];
+push @heightmap => [ (10) x $columns ]; # add extra row of 10s
my $rows = @heightmap;
my $risk_level = 0;
-foreach my $row (0 .. $rows - 2) {
- foreach my $column (0 .. $columns - 2) {
+foreach my $row (0 .. $rows - 2) { # don't care about last row that we added
+ foreach my $column (0 .. $columns - 2) { # don't care about the last column
my $height = $heightmap[$row][$column];
my $lowpoint = 1; # true
foreach my $offset ([0, -1], [0, 1], [-1, 0], [1, 0]) {
if ($height >=
$heightmap[$row + $offset->[0]][$column + $offset->[1]]) {
$lowpoint = 0; # set to false
+ last;
}
}
$risk_level += $height + 1 if $lowpoint;