‹ back to the board

Saturday, November 7

Adeptalgorithms & data structures

The Basilisk Watches of Carrowgate Quarry

Commission of the Carrowgate Stonecutters' Guild, posted at Bitwick

A basilisk has made its den in the deep pit of Carrowgate Quarry. Each night it lifts its head and sweeps its gaze across the quarry floor, sometimes once and sometimes many times, and the sweeps may overlap. The Guild's watchers have recorded every sweep for tonight. They need a reckoning of the stretches when no gaze falls on the floor at all, so that cutters can be sent down to haul out the abandoned marble. A stretch is only worth the risk if it is long enough to do the work.

Write safeWindows(gazes, dusk, k).

Arguments - gazes: a list of pairs [start, end] of integers, in no particular order, with 0 <= start < end <= dusk. Each pair means the floor is under the gaze from minute start up to but not including minute end (a half-open span). Pairs may overlap, nest inside one another, touch end-to-start, or repeat. The list may be empty. - dusk: an integer >= 0. The night is the half-open span from minute 0 up to but not including minute dusk. - k: an integer >= 1, the shortest stretch the cutters will accept.

Return a list of pairs [a, b] of integers: every maximal span [a, b) inside [0, dusk) during which no gaze is active, keeping only those with b - a >= k. A maximal span cannot be extended in either direction without touching a gaze or leaving the night. Order the pairs by a, ascending. If no span qualifies, return an empty list.

Two gazes where one ends at the minute the other starts leave no gap between them.

  • interval merging
  • Adept

examples

  1. case 1gazes of [[2,5],[9,12],[4,7]], dusk of 15, k of 2[[0,2],[7,9],[12,15]] - the first two sweeps overlap into one long watch from 2 to 7
  2. case 2gazes of [[1,10],[2,4],[6,8]], dusk of 14, k of 1[[0,1],[10,14]] - the short sweeps sit wholly inside the long one, so minutes 8 and 9 are still watched
  3. case 3gazes of [[1,2],[3,5],[9,10]], dusk of 12, k of 2[[5,9],[10,12]] - the one-minute gaps at 0 and 2 are too short and are left out
  4. case 4gazes of [] (no sweeps), dusk of 10, k of 3[[0,10]] - the whole night is clear
reward
320 XP
signature
safeWindows
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_unsorted
  • nested_sweeps
  • no_gazes
  • empty_night
  • night_too_short_for_k
  • touching_sweeps
  • short_gaps_filtered
  • fully_watched
  • duplicate_sweeps
  • mixed_chain
  • exact_k_boundary
The Basilisk Watches of Carrowgate Quarry · bitwick