The Weir-Keeper's Tally
The weir-keeper of bitwick reads the mill-pond at dawn and writes down how far it moved overnight: a positive number for a rise, a negative number for a fall, zero for a still night. One number per day, in order, all season long.
The Waterworks Guild settles by the stretch. A stretch is any run of consecutive days, and it is worth the sum of the readings on those days. The Guild names one mark, and pays for every stretch worth exactly that.
Write tallySpans(levels, target). levels is a list of integers, possibly empty, possibly negative. target is an integer. Return an integer: the number of non-empty contiguous spans of levels whose readings sum to exactly target.
Spans are counted by position, so two spans that begin or end on different days are different spans even when their readings are identical. If levels is empty, return 0.
examples
- case 1
levels of [1, 2, 3, 4, 5], target of 52 - days 2-3 (2+3), and day 5 on its own - case 2
levels of [3, -3, 1, 1, -2, 2], target of 04 - case 3
levels of [0, 0, 0, 0], target of 010 - all ten spans of a still season are worth zero
- reward
- 360 XP
- signature
tallySpans
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- the five-day rise
- a season that balances
- no season at all
- one day, matching the mark
- one day, missing the mark
- still water
- a mark below the waterline
- nine days of weather
- the long survey