Apprenticelogic & control flow
The Cistern Warden's Tally
The cistern under the market square is filled by the aqueduct and drawn down by the townsfolk, and the warden keeps a day-by-day log of it. The cistern holds at most capacity buckets and starts each season completely empty.
You are given:
capacity: a non-negative integer, the number of buckets the cistern can hold.deltas: a list of integers, in order. A positive value is buckets poured in that day; a negative value is buckets drawn out; zero is a quiet day.
Write tallyCistern(capacity, deltas). Begin with a level of 0 and apply each entry of deltas in order by adding it to the level. After each entry:
- If the level would rise above
capacity, the excess runs out over the brim: add that excess to the spilled total and set the level tocapacity. - If the level would fall below 0, the missing amount was never there to draw: add that shortfall to the dry total and set the level to 0.
Return a list of exactly three integers: [level, spilled, dry] — the level after the last entry, the total buckets spilled over the brim, and the total buckets that could not be drawn.
examples
- case 1
capacity of 10, entries +6, +6, -20, +3[3, 2, 10] — the level goes 6, then 12 clamped to 10 (2 spilled), then -10 clamped to 0 (10 dry), then 3 - case 2
capacity of 10, entries +10, -10, 0[0, 0, 0] — landing exactly on the brim spills nothing, and draining to exactly empty loses nothing - case 3
capacity of 0, entries +4, -4, +7[0, 11, 4] — nothing can be held, so 4 and 7 spill and the draw of 4 is entirely dry
- reward
- 200 XP
- signature
tallyCistern
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
loading the editor…
- spills then runs dry
- empty log
- zero capacity holds nothing
- exact brim and exact empty
- never touches the walls
- dry first then over the brim
- single overflowing entry