adventofcode/day3/day3b.pm

26 lines
512 B
Perl
Raw Normal View History

2017-12-03 18:08:09 +00:00
use 5.20.0;
use experimental 'postderef';
use Array::Tour::Spiral;
use List::AllUtils qw/ sum /;
my $num = shift;
my $spiral = Array::Tour::Spiral->new( dimensions => $num );
my $grid = [];
my $first = 1;
2017-12-03 18:12:11 +00:00
my $current = 0;
2017-12-03 18:08:09 +00:00
2017-12-03 18:12:11 +00:00
until ( $current > $num ) {
my ($x,$y) = $spiral->next->@*;
$current = $grid->[$x][$y] =
$first ? $first--
: sum map {
my $i = $_;
map { $grid->[$x+$i][$y+$_] } -1..1
} -1..1
2017-12-03 18:08:09 +00:00
}
2017-12-03 18:12:11 +00:00
say $current;