‹ back to the board

Friday, October 16

Apprenticelogic & control flow

The Idol-Haul of Ketterwy Temple

Deep in the jungle, the Ketterwy Temple has begun to sink into its own foundations. The Guild of Reclaimers has bought the right to drag its stone idols out before the roof comes down. Only one tunnel leads out, and it is barely wide enough for a single log-sled. The idols are hauled out on these sleds in the exact order they are pried from their niches.

The haul-master loads the sleds by one fixed rule:

  • Idols are taken strictly in the given order. No idol is ever skipped, held back, or moved ahead of another.
  • An idol goes onto the sled currently being loaded if the sled's total weight, with that idol added, stays at or below capacity.
  • Otherwise, the current sled is sent down the tunnel and a new, empty sled is started with that idol on it.
  • When the idols run out, the sled being loaded is sent as well, provided it carries at least one idol.

Write loadSleds(weights, capacity).

  • weights is a list of positive integers. Each one is the weight of an idol, in the order the idols are recovered. It may be empty.
  • capacity is a positive integer. Every value in weights is at most capacity.

Return a list of sleds in the order they are sent. Each sled is a list of the weights it carries, in loading order. If weights is empty, return an empty list.

  • grouping and partitioning
  • Apprentice

examples

  1. case 1weights of [4, 3, 5, 2, 6], capacity of 10[[4, 3], [5, 2], [6]]. Adding the 5 to the first sled would make 12, so that sled leaves carrying 7.
  2. case 2weights of [5, 6, 1], capacity of 10[[5], [6, 1]]. The 1 would fit beside the 5, but it cannot jump ahead of the 6.
  3. case 3weights of [6, 4, 10, 1], capacity of 10[[6, 4], [10], [1]]. A sled loaded to exactly 10 is allowed.
  4. case 4weights of [], capacity of 5[]. No idols means no sleds.
reward
210 XP
signature
loadSleds
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…
  • basic three sleds
  • exact fill is allowed
  • empty haul
  • single idol at capacity
  • no skipping ahead
  • everything fits on one sled
  • capacity of one
  • longer mixed haul
The Idol-Haul of Ketterwy Temple · bitwick