‹ back to the board

Sunday, October 11

Adeptalgorithms & data structures

The Mirage-Wards of the Saffron Waste

Commission of the Dune-Carters' Guild, posted at the Bitwick inn

The caravan track across the Saffron Waste runs straight from the Bitwick gate, at league 0, to the oasis of Qarsh Amun, at league length. Along it the Guild's hedge-mages have planted wardstones. Each wardstone throws its protection over one stretch of the track, from its start league to its end league. Some stretches overlap, some sit inside others, and some reach past either end of the track. Wherever the protection fails, the mirage-hounds hunt.

The Guild wants the open stretches of track charted before the next caravan sets out.

Write unwardedStretches(length, wards).

  • length is a positive integer: the track covers every league from 0 to length.
  • wards is a list of pairs [start, end] of integers with start < end. Each pair covers every point from start to end, endpoints included. The wards come in no particular order. A ward may start below 0 or end beyond length, and only its part on the track matters. The list may be empty.

Return a list of pairs [a, b], one for each maximal stretch of track with positive length that no ward covers. Each pair satisfies 0 <= a < b <= length. Here a is where the stretch begins: either 0 or the end of a ward. b is where it ends: either the start of a ward or length.

If one ward ends at the same league where another begins, nothing lies open between them. Return the pairs in ascending order of a. If the whole track is covered, return an empty list.

  • interval merging
  • Adept

examples

  1. case 1length of 20, wards [[3, 7], [5, 9], [12, 15]][[0, 3], [9, 12], [15, 20]]. The first two wards overlap into one covered stretch from 3 to 9.
  2. case 2length of 10, wards [[0, 4], [4, 10]][]. The wards meet at league 4, so no open stretch is left.
  3. case 3length of 10, wards [[-5, 2], [8, 15]][[2, 8]]. The parts of the wards that fall off the track are ignored.
reward
320 XP
signature
unwardedStretches
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…
  • overlapping wards merge
  • no wards at all
  • touching wards leave no gap
  • unsorted with nested ward
  • wards overhang both ends
  • wards entirely off the track
  • single ward covers everything
  • one gap in the middle
  • long ward swallows later short ones
  • ward ending exactly at length
The Mirage-Wards of the Saffron Waste · bitwick