Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for [old] Berserk Rook by capback250
"use strict";
function berserkRook(marbles, step) {
const findTop = (pos, data) => {
for (let i = 1 + +pos[1]; i <= 8; i++) {
if (data.indexOf(pos[0] + i) !== -1) return pos[0] + i
}
};
const findBot = (pos, data) => {
for (let i = +pos[1] - 1; i > 0; i--) {
if (data.indexOf(pos[0] + i) !== -1) return pos[0] + i
}
};
const findLeft = (pos, data) => {
for (let i = pos[0].charCodeAt(0) - 1; i >= 97; i--) {
if (data.indexOf(String.fromCharCode(i) + pos[1]) !== -1) return String.fromCharCode(i) + pos[1]
}
};
const findRight = (pos, data) => {
for (let i = pos[0].charCodeAt(0) + 1; i <= 104; i++) {
if (data.indexOf(String.fromCharCode(i) + pos[1]) !== -1) return String.fromCharCode(i) + pos[1]
}
};
const allDirections = (pos, data) => [findTop(pos, data), findBot(pos, data), findLeft(pos, data), findRight(pos, data)].filter(e => e);
let que = [{ kill: [], live: step, pos: marbles}];
for (let node of que) {
let $kill = node.kill.concat(node.pos);
let $live = node.live.filter(e => e !== node.pos);
for (let nextNode of allDirections(node.pos, node.live)) {
if (node.kill.indexOf(nextNode) === -1)
que.push({ kill: $kill, live: $live, pos: nextNode})
}
}
return que.sort((a, b) => b.kill.length - a.kill.length)[0].kill.length
}
May 4, 2017