6 kyu

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 location p2 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 and p2 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 testcases

  • 100 random testcases, testing for correctness of solution

  • All 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.

Puzzles

Stats:

CreatedOct 30, 2018
PublishedOct 31, 2018
Warriors Trained288
Total Skips37
Total Code Submissions255
Total Times Completed27
JavaScript Completions27
Total Stars11
% of votes with a positive feedback rating95% of 10
Total "Very Satisfied" Votes9
Total "Somewhat Satisfied" Votes1
Total "Not Satisfied" Votes0
Total Rank Assessments3
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • JohanWiltink Avatar
Ad