commit 25e9640fd228365b33db3e9bc126cf8287f20acd
parent a884c43af3237e911301caf3397fe73a7108f7c5
Author: Samir Parikh <noreply@samirparikh.com>
Date: Tue, 27 Dec 2022 21:47:43 +0000
solve part 1 of day08
Diffstat:
1 file changed, 36 insertions(+), 7 deletions(-)
diff --git a/day08/day08.pl b/day08/day08.pl
@@ -4,7 +4,7 @@
use strict;
use warnings;
use v5.32;
-#use Data::Dumper;
+use List::Util qw( max );
@ARGV = "input" unless @ARGV;
chomp( my $input = do { local $/; <> } );
@@ -14,16 +14,45 @@ foreach ( split /\n/, $input ) {
push @trees => [ split //, $_ ];
}
-#say Dumper \@trees;
my $rows = scalar @trees;
my $columns = scalar @{ $trees[0] };
-my $visible = ( 2 * $columns ) + 2 * ( $rows - 2 );
-#say $visible;
+my $part1 = ( 2 * $columns ) + 2 * ( $rows - 2 );
+
+my %check = (
+ up => sub {
+ my ( $x, $y ) = @_;
+ return ( $trees[ $y ][ $x ] >
+ max( map { $trees[ $_ ][ $x ] } ( 0 .. $y - 1 ) ) );
+ },
+
+ down => sub {
+ my ( $x, $y ) = @_;
+ return ( $trees[ $y ][ $x ] >
+ max( map { $trees[ $_ ][ $x ] } ( $y + 1 .. $rows - 1 ) ) );
+ },
+
+ left => sub {
+ my ( $x, $y ) = @_;
+ return ( $trees[ $y ][ $x ] >
+ max( map { $trees[ $y ][ $_ ] } ( 0 .. $x - 1 ) ) );
+ },
+
+ right => sub {
+ my ( $x, $y ) = @_;
+ return ( $trees[ $y ][ $x ] >
+ max( map { $trees[ $y ][ $_ ] } ( $x + 1 .. $columns - 1 ) ) );
+ },
+);
for my $y ( 1 .. $rows - 2 ) {
for my $x ( 1 .. $columns - 2 ) {
- #say "$x $y";
- print "$trees[$y][$x] ";
+ foreach my $direction ( qw( up down left right ) ) {
+ if ( $check{ $direction }->( $x, $y ) ) {
+ $part1++;
+ last;
+ }
+ }
}
- say "";
}
+
+say "part 1: ", $part1;