‹ back to the board

Saturday, September 19

Apprenticelogic & control flow

The Toll-Stick of Gorren Bridge

# The Toll-Stick of Gorren Bridge

The keeper of Gorren Bridge records the day on a single stick of yew. For each traveller who crosses, one mark is cut: the number of coins that traveller paid. When a keeper's shift ends, a flat groove is cut instead, the takings are carried off to the strongbox, and the counting begins again from nothing.

The bridge-wardens want to know how far down the stick they must read before a shift's takings first reach the day's quota.

Arguments

  • marks — a list of non-negative integers, read left to right. A value of 0 is a groove; any other value is the coins a traveller paid.
  • quota — a non-negative integer.

Definitions

The *takings* at a position are the sum of all marks from just after the most recent groove up to and including the mark at that position. A groove's own takings are 0, since nothing has been counted since it.

Return

The 1-based position of the earliest mark whose takings are at least quota. If no position on the stick qualifies, return -1.

  • loops with early exit
  • Apprentice

examples

  1. case 1marks of [3, 4, 0, 5, 6], quota of 105 — the takings run 3, 7, 0, 5, 11, and 11 is the first to reach 10
  2. case 2marks of [2, 2, 2], quota of 7-1 — the takings never rise above 6
  3. case 3marks of [0, 5], quota of 01 — the groove's takings are 0, which already meets a quota of 0
reward
220 XP
signature
tollStick
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…
  • shift change before the quota is met
  • quota never reached
  • a groove meets a quota of nothing
  • blank stick
  • first traveller pays it all
  • exact quota after a reset
  • reset spoils the early run
  • grooves only
  • single mark short of the quota
The Toll-Stick of Gorren Bridge · bitwick