Published
Edited
Dec 8, 2018
Insert cell
Insert cell
data = input.split('\n').map(line => line.match(/\d+/g).map(Number))
Insert cell
Insert cell
cloth = {
let cloth = {};
for (let [n, left, top, width, height] of data) {
for (let x = left; x < left + width; x++) {
for (var y = top; y < top + height; y++) {
cloth[`(${x},${y})`] = (cloth[`(${x},${y})`] || 0) + 1;
}
}
}
return cloth;
}
Insert cell
conflictingAreaSize = Object.values(cloth).filter(claims => claims > 1).length
Insert cell
Insert cell
function hasOverlaps(left, top, width, height) {
for (let x = left; x < left + width; x++) {
for (var y = top; y < top + height; y++) {
if (cloth[`(${x},${y})`] > 1) {
return true;
}
}
}
return false;
}
Insert cell
nonOverlappingClaimId = {
for (let [n, left, top, width, height] of data) {
if (!hasOverlaps(left, top, width, height)) {
return n;
}
}
}
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more