‹ back to the board

Saturday, September 5

Apprenticelogic & control flow

The Tide Gate of Marrow Creek

The sluice-keeper of Marrow Creek keeps a ledger of the tide, and the ledger records *changes*, not levels: each entry is how far the water rose (a positive integer) or fell (a negative integer) during one watch. Before the first watch the creek stands at level 0, and the gate is shut.

Write tideGate(deltas, high, low).

  • deltas is a list of integers, the watch-by-watch changes in order.
  • high is an integer, the opening mark.
  • low is an integer, the shutting mark.

Process the entries in order. For each entry, first add it to the current level. Then apply exactly one check:

  • If the gate is shut and the level is greater than or equal to high, the gate opens.
  • Otherwise, if the gate is open and the level is less than or equal to low, the gate shuts.
  • Otherwise nothing changes.

So the gate can change state at most once per entry, and a level that is merely still above high while the gate is already open changes nothing.

Return a list of integers: the 0-based indices of the entries after which the gate opened, in increasing order. Return an empty list if the gate never opens.

  • running state across iterations
  • Apprentice

examples

  1. case 1deltas of [3, 2, -1, -5, 4, 1], high of 5, low of 0[1] — the levels run 3, 5, 4, -1, 3, 4. The gate opens at index 1 (level 5 reaches the mark), shuts at index 3 (level -1 falls to or below 0), and never climbs back to 5 afterwards.
  2. case 2deltas of [5, 0, 1], high of 5, low of 0[0] — the gate opens at index 0. At index 1 the level is still 5, but the gate is already open and 5 is not at or below 0, so nothing changes.
  3. case 3deltas of [], high of 1, low of 0[] — no watches were recorded, so the gate never opens.
reward
200 XP
signature
tideGate
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…
  • opens once then shuts
  • empty ledger
  • opens on the first watch
  • repeated cycling
  • never reaches the mark
  • slow drain to a negative low
  • stays open above the mark
The Tide Gate of Marrow Creek · bitwick