Skip to content

Instantly share code, notes, and snippets.

@ADelRosarioH
Created January 4, 2017 17:49
Show Gist options
  • Save ADelRosarioH/56145f0c60add52956d2adc4452722da to your computer and use it in GitHub Desktop.
Save ADelRosarioH/56145f0c60add52956d2adc4452722da to your computer and use it in GitHub Desktop.
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