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):
pitchesis a list of positive integers, the heights of the pitches from bottom to top. It may be empty.reachis a non-negative integer. A pitch whose height is less than or equal toreachis climbed for free.ropeis 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.
examples
- case 1
pitches 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 - case 2
pitches 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 - case 3
pitches 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
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- 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