Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
reduce solution in Clear category for [old] Ground for the House by Sim0000
"use strict";
function house(plan) {
const map = plan.split('\n').filter(s => s.length > 0);
const xsize = map[0].length, ysize = map.length;
const x0 = map.reduce((m, r) => {let n = r.indexOf('#'); return n >= 0 && n < m ? n : m;}, xsize);
const x1 = map.reduce((m, r) => {let n = r.lastIndexOf('#'); return n >= 0 && n > m ? n : m;}, -1);
const y0 = map.reduce((m, r, i) => r.includes('#') && i < m ? i : m, ysize);
const y1 = map.reduce((m, r, i) => r.includes('#') && i > m ? i : m, -1);
if(x0 == xsize || x1 < 0 || y0 == ysize || y1 < 0) return 0;
return (x1 - x0 + 1) * (y1 - y0 + 1);
}
var assert = require('assert');
if (!global.is_checking) {
console.log('Example:')
console.log(house(`
0000000
##00##0
######0
##00##0
#0000#0
`))
// These "asserts" are used for self-checking and not for an auto-testing
assert.equal(house(`
0000000
##00##0
######0
##00##0
#0000#0
`), 24)
assert.equal(house(`0000000000
#000##000#
##########
##000000##
0000000000
`), 30)
assert.equal(house(`0000
0000
#000
`), 1)
assert.equal(house(`0000
0000
`), 0)
assert.equal(house(`0##0
0000
#00#
`), 12)
console.log("Coding complete? Click 'Check' to earn cool rewards!");
}
Sept. 7, 2018
Comments: