Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for [old] Square Spiral by GazonkFoo
"use strict";
function findDistance(first, second) {
var coord = function(num) {
var d = 0;
var x = 0;
var y = 0;
for(var i=0; i<(num-1); i++) {
if(Math.floor((d+2)/2) * Math.ceil((d+2)/2) == i) {
d++;
}
switch(d%4) {
case 0: y--; break;
case 1: x++; break;
case 2: y++; break;
case 3: x--; break;
}
}
return [x,y];
}
var a = coord(first);
var b = coord(second);
return Math.abs(a[0]-b[0]) + Math.abs(a[1]-b[1]);
}
var assert = require('assert');
if (!global.is_checking) {
assert.equal(findDistance(1, 9), 2, "1st example");
assert.equal(findDistance(9, 1), 2, "2nd example");
assert.equal(findDistance(10, 25), 1, "3rd example");
assert.equal(findDistance(5, 9), 4, "4th example");
assert.equal(findDistance(26, 31), 5, "5th example");
assert.equal(findDistance(50, 16), 10, "6th example");
console.log("Coding complete? Click 'Check' to review your tests and earn cool rewards!");
}
Sept. 29, 2016