adventofcode/2017/day3/day3.pm

24 lines
298 B
Perl
Raw Normal View History

2017-12-03 17:34:10 +00:00
use 5.20.0;
use base 'Exporter';
use experimental 'signatures';
our @EXPORT = qw/ path /;
say path(shift) unless caller;
sub path($x) {
my $i = 1;
return 0 if $x == 1;
$i+=2 until $x <= $i**2;
my $j = ( $x - ($i-2)**2 ) % ($i-1);
($i-1)/2 + abs( $j - ($i-1)/2 );
}
1;