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).
lengthis a positive integer: the track covers every league from0tolength.wardsis a list of pairs[start, end]of integers withstart < end. Each pair covers every point fromstarttoend, endpoints included. The wards come in no particular order. A ward may start below0or end beyondlength, 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.
examples
- case 1
length 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. - case 2
length of 10, wards [[0, 4], [4, 10]][]. The wards meet at league 4, so no open stretch is left. - case 3
length 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
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- 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