The Ember-Shrines of the Isenhal Barrens
Commission of the Lampwardens' Guild, Bitwick chapter
The Isenhal Barrens are a frozen waste where no fire can be lit. Any courier crossing them carries a single ember-lantern, which holds a fixed amount of warmth. The road is marked off in stretches. Walking a stretch drains a set amount of warmth from the lantern. Along the way stand old ember-shrines, where a lantern is rekindled to full.
The Guild wants to know, before a courier sets out, exactly where along a planned route the lantern would fail.
The contract
Write firstColdStretch(cap, stretches).
capis a non-negative integer: the lantern's full warmth. The lantern starts the journey full.stretchesis a list of non-negative integers, the route in walking order.- A value of
0is an ember-shrine. Reaching it sets the lantern's warmth to exactlycap. - Any positive value is a stretch of road costing that much warmth.
The courier takes the entries in order. A stretch can be walked only if its cost is less than or equal to the warmth currently in the lantern. Walking it subtracts the cost from that warmth. A lantern left at exactly 0 warmth has not failed.
Return the 0-based index of the first stretch whose cost is greater than the lantern's current warmth. If the courier reaches the end of the list without that happening, return -1. That includes an empty route.
examples
- case 1
cap of 10, stretches [4, 5, 0, 7, 3, 2]5: warmth goes 10, 6, 1, rekindled to 10, 3, 0, and the stretch costing 2 at index 5 cannot be walked - case 2
cap of 5, stretches [5, 0, 5, 0, 5]-1: the lantern hits exactly 0 three times but never fails - case 3
cap of 3, stretches []-1: there is no road to fail on
- reward
- 220 XP
- signature
firstColdStretch
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- fails after a rekindling
- empty route
- exactly zero is survived
- first stretch too costly
- no shrines runs dry
- consecutive shrines
- zero capacity only shrines
- zero capacity any stretch fails
- stretch larger than full lantern
- completes long route