Created
January 4, 2017 17:49
-
-
Save ADelRosarioH/56145f0c60add52956d2adc4452722da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function stepsToPosition(a, b){ | |
var limit = 1000; | |
var steps = 0; | |
var o = {}; | |
var x = 0, y = 0; | |
var moves = 3; | |
for(var i = 0; i < limit; i++) { | |
var r = (x < a) ? (x + 2 < a ? 2 : 1) : 1; | |
x += r; | |
moves -= r; | |
for(var j = 0; j < limit; j++){ | |
y += moves; | |
break; | |
} | |
moves = 3; | |
steps++; | |
if(x == a) break; | |
if(x > a && y > b) break; | |
} | |
var cantReachB = y != b; | |
steps = x != a || y != b ? -1 : steps > limit ? -2 : steps; | |
return steps; | |
} | |
console.log(stepsToPosition(12, 9)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment