Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
recursive search solution in Clear category for [old] The Buttons by Sim0000
"use strict";
function buttons(ceiling) {
// recursive search connected area & sum
function search(y, x){
if(y < 0 || ysize <= y || x < 0 || xsize <= x || ceiling[y][x] == 0) return 0;
const n = ceiling[y][x]|0;
ceiling[y][x] = 0;
return n + search(y - 1, x) + search(y + 1, x) + search(y, x - 1) + search(y, x + 1);
}
ceiling = ceiling.split('\n').filter(x => x.length > 0).map(x => x.split(''));
const ysize = ceiling.length, xsize = ceiling[0].length;
let result = [];
for(let y = 0; y < ysize; y++){
for(let x = 0; x < xsize; x++){
if(ceiling[y][x] != 0) result.push(search(y, x));
}
}
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. 28, 2018