‹ back to the board

Friday, September 11

Apprenticelogic & control flow

The Bellows of Thistledown Forge

The smith of Thistledown Forge has gone up the hill for the night and left behind the order in which her bellows-boy is to work. The forge's heat is a whole number from 0 to 9, and at dusk it sits at 0.

Write forgeTally(actions), where actions is a list of strings, each one exactly "pump", "rest", or "strike". Work through the list in order:

  • "pump" raises the heat by 3, but the heat never rises above 9.
  • "rest" lowers the heat by 2, but the heat never falls below 0.
  • "strike": if the heat is 6 or more, one ingot is forged and the heat drops by 6. Otherwise no ingot is forged and the heat drops by 1, never falling below 0.

Return a list of two integers: the number of ingots forged, followed by the heat left in the forge after the last action. An empty list of actions returns zero ingots and zero heat.

  • running state across iterations
  • Apprentice

examples

  1. case 1actions of "pump", "pump", "strike"[1, 0] — heat rises 0 to 3 to 6, and the strike forges an ingot and leaves the forge cold
  2. case 2actions of "pump", "strike", "pump", "pump", "strike"[1, 2] — the first strike lands at heat 3, forges nothing and drops the heat to 2; the second lands at heat 8 and forges an ingot
  3. case 3actions of "pump", "pump", "pump", "pump"[0, 9] — the fourth pump is wasted, since the heat is capped at 9
  4. case 4an empty list of actions[0, 0]
reward
200 XP
signature
forgeTally
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…
  • empty night at the forge
  • two pumps and a strike at exactly six
  • a cold strike wastes a point of heat
  • the heat is capped at nine
  • resting a cold forge never goes below zero
  • a full night of work
  • pumping then letting it die down
The Bellows of Thistledown Forge · bitwick