Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
includes solution in Clear category for [old] Chess Knight by Sim0000
"use strict";
function chessKnight(start, moves) {
const dir = [[-1, -2], [-2, -1], [-2, 1], [-1, 2], [1, 2], [2, 1], [2, -1], [1, -2]];
const alpha = "abcdefgh";
const digit = "12345678";
const fromStr = s => [alpha.indexOf(s[0]), digit.indexOf(s[1])];
const toStr = a => alpha[a[0]] + digit[a[1]];
let old = [start];
let result = [];
for(let move = 0; move < moves; move++){
let current = [];
for(let pos of old){
let p = fromStr(pos);
for(let d of dir){
let p0 = [p[0] + d[0], p[1] + d[1]];
if(p0[0] < 0 || 7 < p0[0] || p0[1] < 0 || 7 < p0[1]) continue;
p0 = toStr(p0);
if(result.includes(p0) || current.includes(p0)) continue;
current.push(p0);
}
}
result = result.concat(current);
old = current;
}
return result.sort();
}
var assert = require('assert');
if (!global.is_checking) {
console.log('Example:')
console.log(chessKnight('a1', 1))
// These "asserts" are used for self-checking and not for an auto-testing
assert.deepEqual(chessKnight('a1', 1), ['b3', 'c2'])
assert.deepEqual(chessKnight('h8', 2), ['d6', 'd8', 'e5', 'e7', 'f4', 'f7', 'f8', 'g5', 'g6', 'h4', 'h6', 'h8'])
console.log("Coding complete? Click 'Check' to earn cool rewards!");
}
May 5, 2018