Skip to content

Instantly share code, notes, and snippets.

@willisplummer
Created December 13, 2018 23:17
Show Gist options
  • Save willisplummer/b81e2a5e6b47c4cf0f17e399d6ef2a75 to your computer and use it in GitHub Desktop.
Save willisplummer/b81e2a5e6b47c4cf0f17e399d6ef2a75 to your computer and use it in GitHub Desktop.
const data = [
[151, 671, 11, 15],
[887, 913, 28, 22],
] // etc
// get size of square
const longest = data.reduce((acc, arr) => {
const x = arr[0] + arr[2];
const y = arr[1] + arr[3];
const longerCoord = x > y ? x : y;
return acc > longerCoord ? acc : longerCoord;
}, 0)
// pt 1
const square = new Array(longest + 1).fill(new Array(longest + 1).fill(0))
const applyCoordsToSquare = (square, coords) => {
return square.map((row, i) => {
if (coords[1] <= i && i < (coords[1] + coords[3])) {
return row.map((point, i) => {
if (coords[0] <= i && (i < coords[0] + coords[2])) {
return point + 1;
}
return point;
})
}
return row
})
}
const filledSquare = data.reduce(applyCoordsToSquare, square);
const totalOverlapping = filledSquare.reduce((acc, row) => acc + row.reduce((acc, point) => point > 1 ? acc + 1 : acc, 0), 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment