6 kyu

Progressive Spiral Number Distance

106 of 245GiacomoSorbi

Description:

Final kata of the series (highly recommended to compute layers and branch first to get a good idea), this is a blatant ripoff of the one offered on AoC.

Given a number, return the Manhattan distance considering the core of the spiral (the 1 cell) as 0 and counting each step up, right, down or left to reach a given cell.

For example, using our beloved 5x5 square:

17 16 15 14 13    4 3 2 3 4
18 05 04 03 12    3 2 1 2 3
19 06 01 02 11 => 2 1 0 1 2
20 07 08 09 10    3 2 1 2 3
21 22 23 24 25    4 3 2 3 4

And thus your code should behave like this:

distance(1) === 0
distance(5) === 2
distance(25) === 4
distance(30) === 5
distance(50) === 7
distance(1) == 0
distance(5) == 2
distance(25) == 4
distance(30) == 5
distance(50) == 7
distance(1) == 0
distance(5) == 2
distance(25) == 4
distance(30) == 5
distance(50) == 7
distance(1) == 0
distance(5) == 2
distance(25) == 4
distance(30) == 5
distance(50) == 7
distance(1) == 0
distance(5) == 2
distance(25) == 4
distance(30) == 5
distance(50) == 7
Distance(1) == 0
Distance(5) == 2
Distance(25) == 4
Distance(30) == 5
Distance(50) == 7

Just be ready for larger numbers, as usual always positive.

[Dedicated to swiftest learner I met in a long while]

Mathematics
Number Theory
Fundamentals

Stats:

CreatedDec 6, 2017
PublishedDec 7, 2017
Warriors Trained978
Total Skips99
Total Code Submissions953
Total Times Completed245
JavaScript Completions51
Ruby Completions18
Crystal Completions5
Python Completions106
C++ Completions74
C# Completions30
Total Stars24
% of votes with a positive feedback rating92% of 63
Total "Very Satisfied" Votes53
Total "Somewhat Satisfied" Votes10
Total "Not Satisfied" Votes0
Total Rank Assessments3
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • GiacomoSorbi Avatar
  • Voile Avatar
  • KataSideKick Avatar
  • hobovsky Avatar
  • Just4FunCoder Avatar
  • saudiGuy Avatar
Ad