Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2017
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1.  
  2. const input = 368078;
  3.  
  4. let i = 0;
  5. let ring = 0;
  6. let sum = 1;
  7. let lenghtOfSide = 0;
  8. let lengthOfRing = 0;
  9.  
  10. //find the correct ring the number is one
  11. while(sum < input){
  12. ring++;
  13. i++;
  14. sum = sum + 8 * ring;
  15. lengthOfRing = 8 * ring;
  16. lenghtOfSide = (lengthOfRing + 4 ) / 4;
  17. }
  18.  
  19.  
  20. let coordinates = {
  21. x: ring,
  22. y: -ring,
  23. }
  24.  
  25. //find the position of the ring the number is one
  26. let count = 0;
  27. while(sum != input){
  28. //console.log(coordinates, count,lenghtOfSide,sum );
  29.  
  30. if(count < lenghtOfSide-1)
  31. coordinates.x--;
  32. else if(count < lenghtOfSide*2-2)
  33. coordinates.y++;
  34. else if(count < lenghtOfSide*3-3)
  35. coordinates.x++;
  36. else if(count < lenghtOfSide*4-4)
  37. coordinates.y--;
  38. sum--;
  39. count++;
  40. }
  41.  
  42. console.log(coordinates);
  43.  
  44. const result = Math.abs(coordinates.x) + Math.abs(coordinates.y);//calculate manhattan distance
  45.  
  46. console.log(result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement