aoc2021

Advent of Code 2021 solutions in Perl.
git clone git://git.samirparikh.com/aoc2021
Log | Files | Refs | README

commit a59553c92fd8f85cd09274d8756a2cf61f540b74
parent 7d9faeae1dfb12f9efc9ceb07e99b14623de790b
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Tue, 21 Dec 2021 15:23:24 +0000

initial commit for day09-2.pl

Diffstat:
Aday09-2.pl | 34++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+), 0 deletions(-)

diff --git a/day09-2.pl b/day09-2.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use v5.22; +use Data::Dumper; + +sub find_basin { + my ($row, $column, $heightmap_ref) = @_; +} + +my @heightmap = map{ [m/\d/g, 9] } <>; # add extra column of 9s +my $columns = @{$heightmap[0]}; +push @heightmap => [ (9) x $columns ]; # add extra row of 9s +my $rows = @heightmap; +my $risk_level = 0; + +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; + } + } + # found low point + $risk_level += $height + 1 if $lowpoint; + } +} + +say $risk_level;