From eebed65b3386bb5fc13b8f9687fabcf7d9affc07 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 3 Dec 2017 13:12:11 -0500 Subject: [PATCH] optimization --- day3/day3b.pm | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/day3/day3b.pm b/day3/day3b.pm index 515305b..27f659f 100644 --- a/day3/day3b.pm +++ b/day3/day3b.pm @@ -10,19 +10,16 @@ my $spiral = Array::Tour::Spiral->new( dimensions => $num ); my $grid = []; my $first = 1; +my $current = 0; -while ( my ($x,$y) = $spiral->next->@* ) { - if( $first) { - $grid->[$x][$y] = 1; - $first = 0; - } - else { - $grid->[$x][$y] = sum map { - my $i = $_; - map { $grid->[$x+$i][$y+$_] } -1..1 - } -1..1 - } - - warn $grid->[$x][$y]; - exit say $_ for grep { $_ > $num } $grid->[$x][$y]; +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;