The Coracle-Runs of Hollin Weir
The Hollin Weir is too wild to bridge and too deep to ford. An expedition bound for the far bank must carry its supplies across in coracles, one boat at a time. The bundles are stacked on the near bank in a fixed order and must be loaded in exactly that order: none may be skipped, held back or reordered. Each coracle can safely carry at most cap stone of weight.
The loadmaster fills boats this way. Bundles are taken one by one in order and placed into the coracle currently being loaded. When the next bundle would make that coracle's total weight greater than cap, the coracle is launched, and that bundle becomes the first bundle in a fresh coracle. A coracle that is still empty always accepts the next bundle, even one heavier than cap; such a bundle crosses alone. A load exactly equal to cap is allowed.
Write loadCoracles(weights, cap), where: - weights is a list of positive integers, the bundle weights in stacking order (it may be empty); - cap is a positive integer, the most a coracle may carry under the rule above.
Return a list of coracles in launch order, where each coracle is the list of bundle weights it carries, in their original order. If weights is empty, return an empty list. Every bundle appears in exactly one coracle, and no coracle is empty.
examples
- case 1
weights of [3, 4, 2, 5, 1], cap of 6[[3], [4, 2], [5, 1]] - the 4 would sink the first boat, and 4 + 2 fills the second exactly - case 2
weights of [2, 9, 1, 1], cap of 5[[2], [9], [1, 1]] - the 9-stone bundle is over the limit, so it crosses alone in its own coracle - case 3
weights of [], cap of 5[] - nothing to ferry, no coracles launched
- reward
- 210 XP
- signature
loadCoracles
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- mixed loads with an exact fill
- empty bank
- overweight bundle crosses alone
- each bundle exactly at the cap
- everything fits in one coracle
- single overweight bundle
- longer column of bundles
- overweight bundle in the middle of light ones