Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Clear&Readable solution in Clear category for [old] The Buttons by SaintDron
"use strict";
function buttons(ceiling) {
function calc(r, c) {
let res = ceiling[r][c];
ceiling[r][c] = 0;
[[-1, 0], [0, -1], [0, 1], [1, 0]].forEach(([dy, dx]) => {
if ((ceiling[r + dy] || [])[c + dx]) {
res += calc(r + dy, c + dx);
}
});
return res;
}
let result = [];
ceiling = ceiling.trim().split('\n').map(r => [...r].map(v => +v));
ceiling.forEach((row, r) => {
row.forEach((v, c) => {
if (v) result.push( calc(r, c) );
});
});
return result.sort((a, b) => b - a);
}
var assert = require('assert');
if (!global.is_checking) {
console.log('Example:')
console.log(buttons(`
001203
023001
100220`))
// These "asserts" are used for self-checking and not for an auto-testing
assert.deepEqual(buttons(`
001203
023001
100220`), [8, 4, 4, 1])
assert.deepEqual(buttons(`
000000
000055
000055`), [20])
assert.deepEqual(buttons(`
908070
060504
302010`), [9, 8, 7, 6, 5, 4, 3, 2, 1])
console.log("Coding complete? Click 'Check' to earn cool rewards!");
}
Sept. 23, 2018