const steps = n => {
const root = Math.ceil(Math.sqrt(n)),
curR = root % 2 !== 0 ? root : root + 1,
numR = (curR - 1) / 2,
cycle = n - ((curR - 2) ** 2),
innerOffset = cycle % (curR - 1);
return numR + Math.abs(innerOffset - numR);
};
console.log(steps(325489));
document.getElementsByTagName('pre')[0].innerHTML.trim().split('\n')
.map(line => line.split(/\D+/))
.map(line => Math.max.apply(null, line) - Math.min.apply(null, line))
.reduce((a, b) => a+b, 0)
const code = document.getElementsByTagName('pre')[0].innerHTML.trim();
let sum = 0;
for(let i=1; i<code.length;i++) {
const current = code[i],
prev = code[i-1];
if(current === prev) { sum += parseInt(current, 10) }
}
if(code[0] === code[code.length-1]) sum += parseInt(code[0], 10)
document.getElementsByTagName('pre')[0].innerHTML = sum;