‹ back to the board

Tuesday, October 13

Apprenticelogic & control flow

The Windworn Galleries of Brannoch Deep

Commission of the Lampwardens' Guild of Bitwick

Beneath the mountain of Brannoch lie the Windworn Galleries, a chain of passages where cold drafts howl up from the deep. The Guild sends a lantern-bearer down them ahead of each delving party. Before the party follows, it wants to know how many passages the bearer will walk through in darkness.

The bearer carries one lantern. It has two separate things to track: how much oil it holds, and whether its flame is burning.

Arguments - oil: a non-negative integer. This is the lantern's capacity. The lantern starts full, holding oil units. The flame starts burning if oil > 0 and starts out if oil is 0. - path: a string containing only the characters ., W, F and O. The bearer meets these marks one at a time, from left to right.

What each mark does - . is a passage. If the flame is burning, the passage is lit and the lantern uses 1 unit of oil. If that leaves the lantern with 0 oil, the flame goes out. If the flame is not burning, the passage is dark and no oil is used. - W is a gust. The flame goes out if it was burning. The oil is unchanged. - F is a flame-niche. If the lantern holds at least 1 oil, the flame is relit or stays lit. If the lantern holds 0 oil, nothing happens. - O is an oil cask. The lantern's oil is set back to oil, its full capacity. The flame is not changed, so a cask does not relight an extinguished flame.

Only . marks can be lit or dark. Gusts, niches and casks are never counted.

Return an integer: the number of . passages in path that are dark under these rules. Write darkPassages(oil, path).

  • running state across iterations
  • Apprentice

examples

  1. case 1oil of 2, path "..O.."2. The first two passages burn both units of oil and the flame goes out. The cask refills the oil, but nothing relights the flame, so the last two passages are dark.
  2. case 2oil of 3, path ".W.F.."1. The gust leaves 2 oil but puts out the flame, so the next passage is dark. The niche relights the flame, and the last two passages are lit.
  3. case 3oil of 0, path "..F.O."4. The lantern starts empty and unlit. The niche cannot light it with no oil, and the cask refills it to 0, so all four passages are dark.
reward
220 XP
signature
darkPassages
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…
  • cask_does_not_relight
  • gust_then_niche
  • niche_fails_when_empty
  • empty_path
  • zero_capacity
  • enough_oil_all_lit
  • only_a_gust
  • gust_first_then_relight
  • refill_while_out_then_relight
  • single_unit_capacity
  • runs_dry_after_relight
The Windworn Galleries of Brannoch Deep · bitwick