‹ back to the board

Sunday, November 8

Apprenticelogic & control flow

The Geyser-Chorus of Kessendrith Basin

Deep in Kessendrith Basin lies a field of steam-vents. The Pathwardens' Guild has learned that a sealed stone door at the basin's heart grinds open only in a minute when every geyser on the field erupts at once. They will pay well for a reckoning of the first such minute, before their lantern-oil runs out.

Each geyser is described by a pair [first, period] of integers, where first >= 0 and period >= 1. That geyser erupts at minutes first, first + period, first + 2*period, and so on. It never erupts before minute first.

Write geyserChorus(geysers, limit), where: - geysers is a list of [first, period] pairs, possibly empty. - limit is a non-negative integer: the last minute the expedition can wait.

Return the smallest integer minute m with 0 <= m <= limit at which every geyser in geysers erupts. If no such minute exists in that range, return -1. If geysers is empty, every minute qualifies, so return 0.

  • bounded search
  • Apprentice

examples

  1. case 1geysers [[2, 3], [1, 4]], limit 205 - the first geyser erupts at 2, 5, 8, ...; the second at 1, 5, 9, ...; both at minute 5
  2. case 2geysers [[10, 2], [0, 2]], limit 3010 - minute 0 does not count, because the first geyser has not yet begun erupting
  3. case 3geysers [[4, 6], [0, 8]], limit 15-1 - they first erupt together at minute 16, one minute past the limit
reward
220 XP
signature
geyserChorus
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…
  • two geysers meet at five
  • single geyser first eruption
  • meeting exactly at limit
  • meeting one past limit
  • never align
  • empty field
  • three geysers
  • first eruption beyond limit
  • limit zero all at zero
  • late starter not before first
The Geyser-Chorus of Kessendrith Basin · bitwick