Published
Edited
Dec 13, 2018
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function cellValue (x, y) {
const gridId = x + 10;
return Math.trunc((gridId * y + SERIAL) * gridId / 100) % 10 - 5;
}
Insert cell
sumTable = {
let out = Array(GRIDSIZE+1).fill().map(() => Array(GRIDSIZE+1).fill(0));

for (let x = GRIDSIZE; x > 0; x--) {
for (let y = GRIDSIZE; y > 0; y--) {
out[x-1][y-1] = cellValue(x, y) + out[x][y-1] + out[x-1][y] - out[x][y];
}
}

return out;
}
Insert cell
function squareSubgridSum (x, y, w) {
return sumTable[x-1][y-1] + sumTable[x+w-1][y+w-1] -
sumTable[x-1][y+w-1] - sumTable[x+w-1][y-1];
}
Insert cell
function maxSquareSubgridByWidth (w) {
let maxSum = 0;
let maxX = 0;
let maxY = 0;

for (let x = 1; x <= GRIDSIZE - w; x++) {
for (let y = 1; y <= GRIDSIZE - w; y++) {
const sum = squareSubgridSum(x, y, w);
if (sum > maxSum) {
[maxSum, maxX, maxY] = [sum, x, y];
}
}
}

return [maxX, maxY];
}
Insert cell
part1 = maxSquareSubgridByWidth(3).join(',')
Insert cell
Insert cell
function maxSquareSubgrid () {
let maxSum = 0;
let maxWidth = 0;
let maxX = 0;
let maxY = 0;

for (let w = 1; w <= GRIDSIZE; w++) {
for (let x = 1; x <= GRIDSIZE - w; x++) {
for (let y = 1; y <= GRIDSIZE - w; y++) {
const sum = squareSubgridSum(x, y, w);
if (sum > maxSum) {
[maxSum, maxWidth, maxX, maxY] = [sum, w, x, y];
}
}
}
}

return [maxX, maxY, maxWidth];
}
Insert cell
part2 = maxSquareSubgrid().join(',')
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