‹ back to the board

Thursday, October 1

Apprenticelogic & control flow

The Rope-Pitches of Mount Veyl

The Cairnwrights' Guild has posted a commission. A climber means to scale the north face of Mount Veyl and wants to know, before setting out, how high the face will let them go.

The face is a column of pitches, climbed strictly in order from the bottom. Each pitch has a height in metres. The climber can scale any pitch no taller than their reach bare-handed, at no cost. A taller pitch must be roped. Roping a pitch of height h uses exactly h metres of rope, and that rope stays fixed on the face and cannot be used again. If a pitch is taller than the climber's reach and they have fewer metres of rope left than its height, they cannot climb it. The ascent ends there, and no pitch above it is climbed.

Write climbPitches(pitches, reach, rope):

  • pitches is a list of positive integers, the heights of the pitches from bottom to top. It may be empty.
  • reach is a non-negative integer. A pitch whose height is less than or equal to reach is climbed for free.
  • rope is a non-negative integer, the metres of rope the climber starts with.

Return an integer: the number of pitches the climber completes, counted from the bottom, before the first pitch they cannot climb. If they can climb every pitch, return the length of pitches.

  • loops with early exit
  • Apprentice

examples

  1. case 1pitches of [3, 7, 2, 9, 4], reach of 4, rope of 153 - the 3 and the 2 are free, the 7 uses rope and leaves 8 metres, then the 9 cannot be roped with only 8
  2. case 2pitches of [6, 6, 1], reach of 5, rope of 111 - the second 6 needs 6 metres but only 5 remain, so the easy 1 above it is never reached
  3. case 3pitches of [5, 5, 5], reach of 5, rope of 03 - a pitch exactly as tall as the reach is climbed bare-handed
reward
210 XP
signature
climbPitches
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…
  • mixed face stops at unpayable pitch
  • empty face
  • height equal to reach is free
  • rope exactly enough
  • no skipping past a failed pitch
  • first pitch too tall
  • rope runs dry late
  • zero reach all roped
The Rope-Pitches of Mount Veyl · bitwick