From da03373979b63bffe8ac4fa1d49c2487899d6c09 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Thu, 8 Dec 2022 08:51:26 +0100 Subject: [PATCH] Day 8: Perl-ish solution with closures --- 2022/15.pl | 46 ++++++++++------------------------------------ 2022/16.pl | 49 ++++++++++++++++--------------------------------- 2 files changed, 26 insertions(+), 69 deletions(-) diff --git a/2022/15.pl b/2022/15.pl index f8c3ff1..f33c426 100755 --- a/2022/15.pl +++ b/2022/15.pl @@ -3,48 +3,22 @@ use v5.36; use strict; use experimental 'multidimensional'; +use List::Util qw(none); my @m = map { chomp; [ split // ] } <>; my $xmax = @{ $m[0] }; my $ymax = @m; my $count; -for my $x (1 .. $xmax-2) { for my $y (1 .. $ymax-2) { - my $h = $m[$y][$x]; - say $h; - my $visible = 1; - for my $dx (0 .. $x-1) { - $visible = 0 if $m[$y][$dx] >= $h; - } - if ($visible) { - $count++; next; - } - $visible = 1; - say " $visible"; - for my $dx ($x+1 .. $xmax-1) { - $visible = 0 if $m[$y][$dx] >= $h; - } - if ($visible) { - $count++; next; - } - $visible = 1; - say " $visible"; - for my $dy (0 .. $y-1) { - $visible = 0 if $m[$dy][$x] >= $h; - } - if ($visible) { - $count++; next; - } - $visible = 1; - say " $visible"; - for my $dy ($y+1 .. $ymax-1) { - $visible = 0 if $m[$dy][$x] >= $h; - } - if ($visible) { - $count++; next; - } - $count++ if $visible; +for my $x (1 .. $xmax-2) { + my $visible = sub { none { $m[$_->[1]][$_->[0]] >= $m[$y][$x] } @_; }; + $count++ + if $visible->( map { [ $_, $y ] } 0 .. $x-1 ) + || $visible->( map { [ $_, $y ] } $x+1 .. $xmax-1) + || $visible->( map { [ $x, $_ ] } 0 .. $y-1) + || $visible->( map { [ $x, $_ ] } $y+1 .. $ymax-1) + ; } } -say $count + 2* $xmax + 2*$ymax-4; +say $count + 2*$xmax + 2*$ymax - 4; diff --git a/2022/16.pl b/2022/16.pl index c83a77f..c856236 100755 --- a/2022/16.pl +++ b/2022/16.pl @@ -3,6 +3,7 @@ use v5.36; use strict; use experimental 'multidimensional'; +use List::Util qw(none); my @m = map { chomp; [ split // ] } <>; my $xmax = @{ $m[0] }; @@ -11,39 +12,21 @@ my $ymax = @m; my $max = 0; for my $y (1 .. $ymax-2) { for my $x (1 .. $xmax-2) { - my $h = $m[$y][$x]; - my $mul = 1; - my $count = 0; - for my $dx (reverse (0 .. $x-1)) { - $count++; - last if $m[$y][$dx] >= $h; - } - $mul *= $count; - - $count = 0; - for my $dx ($x+1 .. $xmax-1) { - $count++; - last if $m[$y][$dx] >= $h; - } - $mul *= $count; - - $count = 0; - for my $dy (reverse 0 .. $y-1) { - $count++; - last if $m[$dy][$x] >= $h; - } - $mul *= $count; - - $count = 0; - for my $dy ($y+1 .. $ymax-1) { - $count++; - last if $m[$dy][$x] >= $h; - } - $mul *= $count; - # say "$x $y $h $mul"; - - $max = $mul if $max < $mul; - + my $count = sub { + my $c = 0; + for (@_) { + $c++; + last if $m[$_->[1]][$_->[0]] >= $m[$y][$x]; + } + $c; + }; + my $score = $count->( map { [ $_, $y ] } reverse 0 .. $x-1 ) + * $count->( map { [ $_, $y ] } $x+1 .. $xmax-1) + * $count->( map { [ $x, $_ ] } reverse 0 .. $y-1) + * $count->( map { [ $x, $_ ] } $y+1 .. $ymax-1) + ; + $max = $score if $max < $score; } } say $max; + -- 2.43.5