‹ back to the board

Saturday, September 12

Apprenticelogic & control flow

The Brewhouse Vat at the Sign of the Ram

The brewhouse at the Sign of the Ram keeps one great vat, and the cellarer keeps one journal. Each morning the vat stands empty; through the day the journal records every pour and every draw, in order.

You are given cap, a non-negative integer — the vat's capacity in measures — and events, a list of integers read in order. Starting from a level of 0 measures, handle each entry:

  • A positive entry v is a pour of v measures. The level rises, but never above cap; any measures that do not fit spill onto the floor and are counted.
  • A negative entry v is a draw of -v measures. If the vat holds at least that many, the level falls by that amount. Otherwise the draw is refused: the level does not change, and the refusal is counted.
  • An entry of 0 changes nothing and counts as neither a spill nor a refusal.

Return a list of exactly three integers: the final level in the vat, the total number of measures spilled across the day, and the number of refused draws.

  • accumulating over a sequence
  • Apprentice

examples

  1. case 1cap of 10, events of 4, 8, -5, -9, 3[8, 2, 1] — pour 4 (level 4), pour 8 (level 10, 2 spilled), draw 5 (level 5), draw 9 is refused, pour 3 (level 8)
  2. case 2cap of 0, events of 5, -1, 0[0, 5, 1] — nothing fits, so all 5 measures spill; the draw of 1 is refused; the 0 does nothing
  3. case 3cap of 5, events of 5, -5, -1[0, 0, 1] — the vat fills exactly, the draw of 5 empties it exactly, and the last draw is refused
reward
200 XP
signature
vatTally
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…
  • a full day of pours and draws
  • a vat with no capacity at all
  • an empty journal
  • exact fill and exact draw
  • slow filling then overflow
  • idle entries change nothing
  • repeated refusals
The Brewhouse Vat at the Sign of the Ram · bitwick