Apprenticelogic & control flow
The Strongbox of the Wool Guild
The Wool Guild keeps one iron strongbox by the counting-room door, and at dusk the clerk must write three numbers in the margin of the day's page. The page itself is a column of figures: money paid in, money paid out, and the marks the courier leaves when the vault cart comes round.
Write tallyStrongbox(start, entries).
startis an integer, zero or greater: the coins in the strongbox at dawn.entriesis a list of integers, handled in order from first to last.
Each entry is handled by exactly one of these rules:
- If the entry is greater than zero, that many coins are added to the strongbox.
- If the entry is less than zero, a payout of that many coins (its absolute value) is due. If the strongbox currently holds at least that many coins, they are removed. Otherwise the entry is refused: the strongbox is left untouched, and the refusal is counted.
- If the entry is zero, the courier comes: half of the coins currently in the strongbox, rounded down, are removed and carried to the vault. Coins carried off this way add to the vault total, which begins at zero.
Return a list of three integers, in this order: the coins left in the strongbox, the total coins carried to the vault, and the number of refused entries.
examples
- case 1
start of 10, entries of [5, -3, -20, 0][6, 6, 1] — 10 becomes 15, then 12; the payout of 20 is refused because only 12 coins are held; the courier carries off 6, leaving 6 - case 2
start of 7, entries of [0, 0][2, 5, 0] — the first courier takes 3 and leaves 4, the second takes 2 and leaves 2, so the vault has 5 - case 3
start of 3, entries of [-3, -1][0, 0, 1] — a payout of exactly the whole box is allowed, so the second payout has nothing to draw on and is refused
- reward
- 200 XP
- signature
tallyStrongbox
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
loading the editor…
- mixed day with one refusal
- empty page
- two couriers, odd hoard
- exact payout then refusal
- courier on an empty box
- long ledger
- all payouts refused