From bf69af5e3049e06aedde688f1958d66cd4cb5232 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 3 Dec 2017 12:34:10 -0500 Subject: [PATCH] first part --- day3/day3.pm | 23 +++++++++++++++++++++++ day3/day3.t | 10 ++++++++++ 2 files changed, 33 insertions(+) create mode 100644 day3/day3.pm create mode 100644 day3/day3.t 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;