Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
0(1) - no overhead solution in Clear category for [old] Square Spiral by vincent.tscherter
"use strict";
function findDistance(a, b) {
var c = function(n) {
var a = Math.ceil(Math.sqrt(n)), b = a * (a - 1), e, f;
[e, f] = (a%2==0) ? [a / 2, 1] : [(a - 1) / 2, -1];
return n<= b ? [f*(e+n-b-1), f*e] : [f*e, f*(e+b-n+1)];
}; return a = c(a), b = c(b), 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!");
}
Jan. 3, 2017
Comments: