diff --git a/day3/day3.pm b/day3/day3.pm new file mode 100644 index 0000000..372aae6 --- /dev/null +++ b/day3/day3.pm @@ -0,0 +1,23 @@ +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; diff --git a/day3/day3.t b/day3/day3.t new file mode 100644 index 0000000..a35e1bc --- /dev/null +++ b/day3/day3.t @@ -0,0 +1,10 @@ +use Test::More; + +use day3; + +is path(12) => 3; +is path(23) => 2; +is path(1024) => 31; +is path(1) => 0; + +done_testing;