Public
Edited
Sep 13, 2023
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
minima = {
let m = []

for (let y = 0; y < grid.length; ++y) {
for (let x = 0; x < grid[0].length; ++x) {
let min_nbr = Math.min(...[[-1,0],[1,0],[0,-1],[0,1]].map(([ny,nx]) => (grid[y+ny] || [])[x+nx]).filter(e=>e!=undefined));
if (grid[y][x] < min_nbr)
m.push([y,x])
}
}

return m;
}
Insert cell
basins = Object.fromEntries(minima.map(m => {
let b = [];
function ffill(x, y) {
if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length && !b.includes(x + y * grid.length) && grid[x][y] < 9) {
b.push(x + y * grid.length);
ffill(x+1,y);
ffill(x-1,y);
ffill(x,y+1);
ffill(x,y-1);
}
}

ffill(m[0], m[1]);

return [m.toString(), b.map(c => [c % grid.length, Math.floor(c / grid.length)])];
}))
Insert cell
function flow(sx,sy) {
let minnbr = (x,y) => [[-1,0],[1,0],[0,-1],[0,1],[0,0]].map(([nx,ny]) => [(grid[x+nx] || [])[y+ny], [x+nx,y+ny]]).filter(e=>e[0]!=undefined).reduce((a,b) => a[0] < b[0] ? a : b)

let walk = [[sx,sy]];
let x = sx, y = sy;
let min = minnbr(x,y);
while (min[1].toString() != [x,y].toString()) {
[x,y] = min[1];
walk.push(min[1])
min = minnbr(x,y);
}
return walk;
}
Insert cell
import {heatmap} from "@fil/heatmap"
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