Task 2 of 6
Rain has landed. Where does it go? Downhill — but "downhill" on a grid needs spelling out, and two details decide whether this simulation ever grows a river.
One: the surface is rock plus water. A puddle sitting in a hollow
raises the level the next drop has to climb; keep filling it and the puddle spills over
the rim by itself. Route on height alone and hollows become bottomless
traps. So every comparison below is between height + water values — call
that H.
Two: square the drops. This pass adds up
d² over the neighbours that are below this cell, and the next task
will hand each of them d²/total of the water. Weighting by the plain drop
smears the flow across every neighbour that is even slightly lower and no channel ever
forms; squaring makes a cell commit to the steep side. That one exponent is the
difference between a damp hillside and a river.
dropTotal so each cell returns the sum
of d² over its four neighbours, counting only the ones the water can
actually fall to.height[y][x] + water[y][x], not height aloned = here − there; add d * d only when d > 0The starter writes the left neighbour out in full. The other three are the same two lines with different indexes:
d = here - (height[y][xr] + water[y][xr]);
if (d > 0) total += d * d;d * d is positive whether the neighbour is above or below, so the
square destroys the very information you are selecting on. The if (d > 0)
has to come first, or an uphill neighbour ends up "receiving" water.
This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.