‹ back to the board

Tuesday, September 22

Apprenticelogic & control flow

The Lamplighter's Round

The lamplighter of bitwick walks one lane each dusk, carrying a can of oil from the cistern at the lane's mouth.

You are given oil, a non-negative integer: the units of oil in the can when the round begins, which is also the can's full capacity. You are given lamps, a list of positive integers: the units each lamp along the lane needs, in the order the lamps are passed.

The lamps are visited once, in order, and these rules apply to each in turn:

  • If the can currently holds at least as much oil as the lamp needs, that amount is poured out of the can and the lamp counts as lit.
  • Otherwise nothing is poured and the lamp counts as dark.
  • If a lamp is dark and the lamp immediately before it on the lane was also dark, the can is refilled to oil units at once, before moving on. A refill also clears the run of dark lamps, so the dark lamp after a refill is not itself a second dark in a row.

A refill happens even when the second dark lamp of a pair is the final lamp on the lane.

Write lampRound(oil, lamps), which returns a list of exactly two integers: the number of lamps lit, followed by the number of refills made.

  • running state across iterations
  • Apprentice

examples

  1. case 1oil of 5, lamps of [2, 4, 3, 1, 5][2, 1] - the 2 and the 3 are lit, leaving the can empty; the 1 and the 5 are both dark, so the can is refilled after the last lamp
  2. case 2oil of 2, lamps of [5, 5, 5, 5][0, 2] - every lamp is dark; the second causes a refill and clears the run, then the fourth causes another
  3. case 3oil of 10, lamps of [][0, 0]
reward
210 XP
signature
lampRound
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…
  • lit, dark, and a refill at the lane's end
  • empty lane
  • every lamp lit, no refills
  • four dark lamps give two refills
  • empty can from the start
  • refill revives the round
  • lit lamp between darks prevents a refill
  • three darks after an early refill
The Lamplighter's Round · bitwick