‹ back to the board

Saturday, October 24

Adeptalgorithms & data structures

The Glowcap Warrens of Umbersett

Commission of the Lampless Guild, Umbersett

Beneath the hill-town of Umbersett lies a warren of caverns joined by narrow tunnels. Things that hate the light have nested in the dark ones. The Guild means to flood the whole warren with the pale light of glowcap fungus. Spores are planted in a few caverns, and the glow creeps outward through the tunnels night by night. Before the delvers go down, the Guild must know how many nights to wait, or whether some cavern will stay dark forever.

The rules of the spread are exact:

  • On night 0, every seeded cavern glows, and no other cavern does.
  • On each following night, every cavern joined by a tunnel to a cavern that was already glowing begins to glow.
  • A glowing cavern never goes dark.

Write glowNights(n, tunnels, seeds).

  • n is an integer, n >= 1. It is the number of caverns, numbered 0 to n - 1.
  • tunnels is a list of pairs [a, b]. Each pair is a tunnel joining caverns a and b, with a != b. A tunnel can be crossed in both directions. The same pair may appear more than once, in either order. The list may be empty.
  • seeds is a list of cavern numbers where spores are planted. Numbers may repeat. The list may be empty.

Return an integer:

  • the smallest night number on which every cavern is glowing, or
  • -1 if some cavern never comes to glow.

If every cavern is seeded, the answer is 0.

  • graph traversal
  • Adept

examples

  1. case 1n of 7, tunnels [[0,1],[0,2],[1,3],[2,3],[3,4],[4,5],[5,6],[6,0]], seeds [3]3. Caverns 1, 2 and 4 glow on night 1, caverns 0 and 5 on night 2, and cavern 6 on night 3.
  2. case 2n of 6, tunnels [[0,1],[1,2],[2,3],[3,4],[4,5]], seeds [0, 5]2. The glow spreads in from both ends and meets in the middle.
  3. case 3n of 5, tunnels [[0,1],[1,2],[2,3]], seeds [0]-1. Cavern 4 has no tunnels and no spores, so it stays dark forever.
reward
320 XP
signature
glowNights
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…
  • single seed down a chain
  • two seeds meet in the middle
  • loops and branches
  • disconnected warren
  • isolated unseeded cavern
  • no seeds at all
  • single seeded cavern
  • every cavern seeded
  • repeated seeds and tunnels
The Glowcap Warrens of Umbersett · bitwick