Simple Fun #397: The Distance Between Two Points in a Navigational Map
Description:
Task
You walk on the road of underground tunnel. You need to find the way to the destination.
Fortunately, there is a navigation system on your cell phone that shows all the paths.
Looking at the map, you know the following things:
There is only one road. no distributor road.
As you can see, the shape of the road is a spiral hexagon.
In order to adapt to the road shape, navigation map uses a special coordinate system. The x axis is horizontal and the y axis is tilted(see the image above).
You see your current location
p1
and destination locationp2
on the map.The distance from every two adjacent points on the road is
1
unit. point(0,0) to point(-1,0) is 1, point(-1,0) to point(-1,-1) is 1, point(-1,-1) to point(0,-1) is 1, and so on..
Your task is to calculate the distance between p1
and p2
. The result should be a non negative integer.
Note
p1
andp2
both are integer arrays of two elements, such as[0,1], [-1,-2], etc..
The first element means x coordinate, and the second element means y coordinate.0 <= | x coordinate,y coordinate | <= 10^6
7
fixed testcases100
random testcases, testing for correctness of solutionAll inputs are valid.
If my reference solution gives the wrong result in the random tests, please let me know(post an issue).
Example
For p1=[0,0], p2=[1,1]
, the output should be 5
.
For p1=[0,0], p2=[2,2]
, the output should be 16
.
For p1=[1,0], p2=[0,1]
, the output should be 2
.
For p1=[-1,-1], p2=[1,1]
, the output should be 3
.
For p1=[1,1], p2=[-1,-1]
, the output should be 3
too.
Similar Kata:
Stats:
Created | Oct 30, 2018 |
Published | Oct 31, 2018 |
Warriors Trained | 288 |
Total Skips | 37 |
Total Code Submissions | 255 |
Total Times Completed | 27 |
JavaScript Completions | 27 |
Total Stars | 11 |
% of votes with a positive feedback rating | 95% of 10 |
Total "Very Satisfied" Votes | 9 |
Total "Somewhat Satisfied" Votes | 1 |
Total "Not Satisfied" Votes | 0 |
Total Rank Assessments | 3 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 6 kyu |
Lowest Assessed Rank | 7 kyu |