adventofcode/day3/day3b.pm
Yanick Champoux eebed65b33 optimization
2017-12-03 13:12:11 -05:00

26 lines
512 B
Perl

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;
my $current = 0;
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
}
say $current;