The Sluice-Keeper's Cistern Ledger
The sluice-keeper above bitwick tends a stone cistern and writes one number in her ledger each day: the water that ran in, or (as a negative number) the water the town drew out. The cistern holds only so much. Water that arrives when the cistern is already brimming runs over the lip and is lost down the hill; water the town asks for when the cistern is dry is simply never delivered. She wants the season's totals.
Write tallyCistern(cap, start, flow).
capis an integer, the cistern's capacity,cap >= 0.startis an integer, the level at the start of the season, with0 <= start <= cap.flowis a list of integers, the ledger entries in order. Each may be positive, negative, or zero.
The level begins at start. Process the entries in order. For each entry, add it to the level. If the level is then above cap, the excess above cap is spilled and the level becomes cap. If the level is then below 0, the amount below 0 is a shortfall and the level becomes 0.
Return a list of three integers: the level after the last entry, the total spilled over the whole season, and the total shortfall over the whole season, in that order. With an empty flow, return the starting level and two zeros.
examples
- case 1
cap of 10, start of 4, flow of [3, 5, -20, 6][6, 2, 10] — the level runs 7, then 12 (2 spilled, held at 10), then -10 (shortfall of 10, held at 0), then 6 - case 2
cap of 12, start of 0, flow of [12, 1, -13, 2][2, 1, 1] — filling to exactly 12 spills nothing; the next entry spills 1, and the draw leaves a shortfall of 1 - case 3
cap of 0, start of 0, flow of [4, -4][0, 4, 4] — a cistern that holds nothing spills everything it receives and delivers nothing - case 4
cap of 5, start of 5, flow of [][5, 0, 0]
- reward
- 200 XP
- signature
tallyCistern
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- spill then shortfall then refill
- exact brim is not a spill
- empty ledger
- zero capacity
- repeated dry draws then overfill
- single idle day
- steady season without limits