The Beetle-Packs of the Sporewold Descent
Deep beneath Bitwick lies the Sporewold, a cavern-forest of glowing fungus where the Lampwright Company digs for old relics. Relics come up the tunnel one at a time, in the order they are found. Each one is strapped onto a great cave beetle. A beetle can bear at most limit stone of weight, and once it is sent up the shaft, no more relics can be added to it. The Company pays a bounty to whoever writes out the loading tally.
Write packBeetles(weights, limit).
weightsis a list of positive integers: the relics' weights in stone, in the order they are found. It may be empty.limitis a positive integer: the most weight one beetle may carry.
Process the relics in order, filling one beetle at a time. The current beetle starts empty. For each relic:
1. If its weight is greater than limit, it is left behind. It is not loaded, and the current beetle stays open. 2. Otherwise, if the current beetle's total weight plus this relic is at most limit, load it onto the current beetle. 3. Otherwise, send the current beetle off and load this relic onto a fresh beetle, which becomes the current one.
After the last relic, the current beetle is sent off too, unless it is empty.
Return a list of loads, one per beetle sent off, in the order they were sent. Each load is a list of the relic weights on that beetle, in the order they were loaded. If no relic is carried, return an empty list.
examples
- case 1
weights of [4, 3, 2, 5, 1], limit of 6[[4], [3, 2], [5, 1]]: 4 + 3 would be 7, so the first beetle leaves with only the 4 - case 2
weights of [3, 10, 3], limit of 6[[3, 3]]: the 10 is left behind, and the first beetle stays open for the second 3 - case 3
weights of [7, 8], limit of 5[]: nothing can be carried
- reward
- 210 XP
- signature
packBeetles
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- mixed loads
- empty tunnel
- oversized relics skipped
- nothing carriable
- even splits with remainder
- single relic at exact limit
- full-limit relics force new beetles
- skip does not close beetle