The Ember-Pouches of the Skaldrin Icefield
Contract posted by the Hearthward Company, for any party bold enough to cross the Skaldrin Icefield.
The crossing takes many days, and the only thing between a traveller and the killing cold is the ember-pouch sewn into each coat. Every day on the ice changes the pouch's heat. A day of good fuel and shelter raises it, and a day of open wind drains it. The Company wants a reckoning of how many days a traveller spent with the pouch gone completely cold.
The pouch's heat (its *warmth*) is a whole number with a floor and a ceiling:
- It never rises above
cap. Any heat beyondcapis lost. - It never falls below
0. Any loss beyond0is ignored.
Write frozenDays(start, cap, days) where:
startis an integer, the warmth before the first day, with0 <= start <= cap.capis a non-negative integer, the most warmth the pouch can hold.daysis a list of integers, one per day in order. Each is that day's change in warmth. It may be positive, negative or zero.
Process the days in order. For each day:
1. Add the day's change to the current warmth. 2. If the result is greater than cap, set it to cap. 3. If the result is less than 0, set it to 0.
The warmth after these steps carries into the next day.
Return an integer: the number of days after which the warmth is exactly 0. The starting warmth is not a day and is never counted. If days is empty, return 0.
examples
- case 1
start 5, cap 5, days [3, -5, 2]1. The +3 would make 8 but is held at 5. The -5 then brings warmth to 0, which counts. The +2 brings it to 2. - case 2
start 2, cap 10, days [-5, -1, 4, -4]3. Warmth goes to 0 (floored from -3), then 0 again (floored from -1), then 4, then 0. That is three cold days. - case 3
start 0, cap 10, days []0. There are no days, so nothing is counted, even though the starting warmth is 0.
- reward
- 210 XP
- signature
frozenDays
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- cap clamp then cold
- no days
- repeated floor
- never cold
- zero cap always cold
- mixed clamps
- exact landing on zero
- lost overflow matters