Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for [old] Ground for the House by nlupton
"use strict";
function house(plan) {
const start = c => c.indexOf('#') + 1
const end = c => c.lastIndexOf('#') + 2
const boundary = plan.trim()
.split('\n')
.reduce( (t,c,i) =>
c.includes('#') ?
{
minX: t.minX ? Math.min( t.minX, start(c) ) : start(c),
maxX: Math.max( t.maxX, end(c) ),
minY: t.minY ? t.minY : i + 1,
maxY: i + 2
} :
t
, { minX: 0, maxX: 0, minY: 0, maxY: 0})
const houseSize = ({maxX, minX, maxY, minY}) => (maxX - minX) * (maxY - minY)
return houseSize(boundary)
}
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!");
}
Oct. 4, 2018