‹ back to the board

Friday, October 9

Apprenticelogic & control flow

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).

  • cap is a non-negative integer: the lantern's full warmth. The lantern starts the journey full.
  • stretches is a list of non-negative integers, the route in walking order.
  • A value of 0 is an ember-shrine. Reaching it sets the lantern's warmth to exactly cap.
  • 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.

  • loops with early exit
  • Apprentice

examples

  1. case 1cap 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
  2. case 2cap of 5, stretches [5, 0, 5, 0, 5]-1: the lantern hits exactly 0 three times but never fails
  3. case 3cap of 3, stretches []-1: there is no road to fail on
reward
220 XP
signature
firstColdStretch
not yet run

TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.

loading the editor…
  • 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
The Ember-Shrines of the Isenhal Barrens · bitwick