Apprenticelogic & control flow
The Beacon-Keeper's Oil Ledger
The beacon on Bitwick Head has burned every night for ninety years, and the keeper writes one number in her ledger for each thing she does to the oil tank. The tank holds at most cap units and begins the night empty.
You are given two arguments:
cap: a non-negative integer, the capacity of the tank in units.log: a list of integers, the ledger entries in the order they were written.
Each entry is applied to the tank in order:
- A positive entry
npoursnunits in. Whatever does not fit runs over the rim and is lost; the tank is left full. - A negative entry
nburns-nunits. If the tank holds less than that, it burns everything it has and is left empty; the units it could not supply are the shortfall. - An entry of
0changes nothing.
Write tallyOil(cap, log), which returns a list of exactly two integers: the total number of units spilled over the rim across the whole ledger, followed by the total shortfall across the whole ledger.
examples
- case 1
cap of 10, log of [4, -6, 8, 3, -20][1, 12] — the tank goes 0 -> 4, then burns 6 with only 4 present (empty, shortfall 2), then 8, then 11 which spills 1 and rests at 10, then burns 20 with only 10 present (empty, shortfall 10) - case 2
cap of 0, log of [7, -3][7, 3] — nothing can be held, so all 7 poured units spill and all 3 burn units are short - case 3
cap of 5, log of [][0, 0]
- reward
- 200 XP
- signature
tallyOil
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
loading the editor…
- mixed night on the headland
- empty ledger
- tank with no capacity
- repeated overfilling then a long burn
- burning before any oil arrives
- single entry that fits exactly
- zero entries change nothing