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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more