‹ back to the board

Sunday, September 20

Apprenticelogic & control flow

The Cistern-Keeper's Tally at Tidewell

The stone cistern below Tidewell holds rain and gives it back to the village, and the keeper must file a tally at the end of each season.

You are given cap, a non-negative integer, the cistern's capacity in barrels, and flow, a list of integers read from the keeper's log in order. A positive entry is that many barrels of rain arriving; a negative entry is a demand for that many barrels to be drawn out; zero is a quiet day.

The cistern starts empty. Process the entries in order. For each entry, add it to the current level, then:

  • If the result is above cap, the excess above cap runs out of the overflow channel and is lost; the level becomes cap.
  • If the result is below 0, the part below 0 is demand that could not be met; the level becomes 0.
  • Otherwise the level is exactly the result.

Write cisternTally(cap, flow), which returns a list of exactly three integers: the level in the cistern after the last entry, the total number of barrels lost to overflow across the whole log, and the total number of barrels of demand that went unmet across the whole log — in that order.

An empty log returns three zeros.

  • accumulating over a sequence
  • Apprentice

examples

  1. case 1cap of 10, flow of [6, 7, -20, 3][3, 3, 10] — the level runs 6, then 13 clipped to 10 (3 lost), then -10 clipped to 0 (10 unmet), then 3
  2. case 2cap of 0, flow of [4, -2, 7][0, 11, 2] — nothing can ever be held, so 4 and 7 are lost and the demand of 2 is unmet
  3. case 3cap of 100, flow of [10, -3, 5, -12][0, 0, 0] — the level never leaves the range, and ends exactly empty
reward
210 XP
signature
cisternTally
not yet run

TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.

loading the editor…
  • season with both a spill and a drought
  • empty log
  • capacity of zero
  • never touches either bound
  • small cistern, spill then drought
  • quiet days only
  • single entry filling exactly to the brim
  • repeated overflow keeps the level pinned
The Cistern-Keeper's Tally at Tidewell · bitwick