Skip to content

Instantly share code, notes, and snippets.

@dkaraush
Last active December 11, 2025 11:39
Show Gist options
  • Select an option

  • Save dkaraush/d7baeded846802426c22fa60cbda7886 to your computer and use it in GitHub Desktop.

Select an option

Save dkaraush/d7baeded846802426c22fa60cbda7886 to your computer and use it in GitHub Desktop.
Object.getOwnPropertyNames(Math).map(k => window[k] = Math[k])
const input = document.querySelector('pre').innerText.trim().split(`\n`).map(l => l.split(',').map(x => parseInt(x)))
const maxArea = (filter = () => true) =>
input.reduce((acc, [x1,y1], i) => max(acc, input.slice(i+1).reduce((acc, [x2,y2]) =>
max(acc, filter(x1,y1,x2,y2) ? (abs(x2-x1)+1) * (abs(y1-y2)+1) : 0), 0)
), 0)
const xs = input.reduce((arr, [x,y]) => arr.indexOf(x) >= 0 ? arr : [...arr, x], []).sort((a,b)=>a-b)
const ys = input.reduce((arr, [x,y]) => arr.indexOf(y) >= 0 ? arr : [...arr, y], []).sort((a,b)=>a-b)
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
canvas.width = xs.length
canvas.height = ys.length
ctx.beginPath()
for (const [[x1,y1], [x2,y2]] of input.map((c, i, a) => [c, a[i+1>=a.length ? 0 : i+1]])) {
ctx.lineTo(xs.indexOf(x1), ys.indexOf(y1))
ctx.lineTo(xs.indexOf(x2), ys.indexOf(y2))
}
ctx.fillStyle = 'white'
ctx.fill()
document.body.appendChild(canvas)
const bitmap = ctx.getImageData(0, 0, canvas.width, canvas.height).data
const isFilled = (
x1, y1, x2, y2,
ix1 = xs.indexOf(min(x1,x2)), iy1 = ys.indexOf(min(y1,y2)),
ix2 = xs.indexOf(max(x1,x2)), iy2 = ys.indexOf(max(y1,y2))
) => {
for (let x = ix1; x < ix2; ++x)
for (let y = iy1; y < iy2; ++y)
if (bitmap[(x + canvas.width * y) * 4] == 0)
return false
return true
}
console.log({ result1: maxArea(), result2: maxArea(isFilled) })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment