‹ back to the board

today · Wednesday, October 28

Apprenticelogic & control flow

The Storm-Eggs of Halvenreach Aerie

Commission of the Halvenreach Aerie Wardens

The Thunder-Griffins of Halvenreach Aerie lay eggs that crackle with stored lightning. Before the hatching season, the Wardens walk the long nesting ledge and record the charge of every egg, in the order the eggs sit along the ledge. Eggs are then gathered into warding-baskets. Eggs that sit side by side share a basket while their charges are close. A sharp jump in charge between two neighbouring eggs means a fresh basket must begin. The Wardens seek a steady-handed adventurer to sort the eggs before the first storm breaks.

The contract, plainly:

Write basketEggs(charges, limit).

  • charges is a list of non-negative integers, the charge of each egg in ledge order. It may be empty.
  • limit is a non-negative integer.

Split charges into consecutive runs, keeping the original order. Two neighbouring eggs belong to the same run exactly when the absolute difference of their charges is less than or equal to limit. Otherwise, a new run starts at the second of the two. Only neighbouring eggs are compared, so a run's charges may drift far from the value it started at.

Return a list of runs. Each run is a list of integers. Every egg appears exactly once, in its original order. If charges is empty, return an empty list.

  • grouping and partitioning
  • Apprentice

examples

  1. case 1charges of [3, 5, 6, 12, 13, 4], limit of 2[[3, 5, 6], [12, 13], [4]]: the jumps from 6 to 12 and from 13 to 4 each begin a new basket
  2. case 2charges of [0, 3, 6, 9, 12], limit of 3[[0, 3, 6, 9, 12]]: each neighbour is within 3, even though the run drifts from 0 to 12
  3. case 3charges of [5, 9, 14], limit of 4[[5, 9], [14]]: a gap of exactly 4 stays together, but a gap of 5 splits
  4. case 4charges of [] (an empty ledge), limit of 3[]: no baskets at all
reward
210 XP
signature
basketEggs
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 ledge
  • empty ledge
  • single egg
  • zero limit groups equal charges
  • drifting run stays whole
  • every neighbour splits
  • gap equal to limit stays together
  • descending charges
The Storm-Eggs of Halvenreach Aerie · bitwick