commit 5c8e94d2fcf4dddc3ce01d3b7c507e1c1876581d
parent 928d2685b89788eb2f61823608fb015c0afdf13f
Author: Samir Parikh <noreply@samirparikh.com>
Date: Sun, 19 Dec 2021 22:17:29 +0000
update day09-1.pl to provide working solution for Day 09 Part 1
Diffstat:
1 file changed, 28 insertions(+), 0 deletions(-)
diff --git a/day09-1.pl b/day09-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use v5.22;
+use Data::Dumper;
+
+my @heightmap = map{ [m/\d/g, 10] } <>;
+my $columns = @{$heightmap[0]};
+push @heightmap => [ (10) x $columns ];
+my $rows = @heightmap;
+my $risk_level = 0;
+
+foreach my $row (0 .. $rows - 2) {
+ foreach my $column (0 .. $columns - 2) {
+ 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
+ }
+ }
+ $risk_level += $height + 1 if $lowpoint;
+ }
+}
+
+say $risk_level;