The Moonwarg Vigil of Pellacombe
Above the hill-village of Pellacombe hang two moons, pale Ilse and red Morrow. The Moonwarg pack of the Pellacombe barrows only leaves its dens on a night when both moons are full. The Wardens of Pellacombe are hiring hunters to lie in wait on that night, and they need to know which night it will be before their watch runs out.
Nights are numbered from 0 (tonight). Ilse is full on nights p, p + a, p + 2a, p + 3a, … and on no other night. Morrow is full on nights q, q + b, q + 2b, … and on no other night. No moon is full on any night before its first full night.
Write firstDoubleMoon(a, p, b, q, limit), where:
aandbare integers, each at least1: the number of nights between consecutive full nights of Ilse and of Morrow.pandqare integers, each at least0: the first night on which Ilse and Morrow are full. They may be larger thanaorb.limitis an integer, at least0: the last night of the Wardens' watch.
Return the smallest night n with 0 <= n <= limit on which both moons are full. If there is no such night within the watch, return -1. The bound is inclusive: a double full moon on night limit itself counts.
examples
- case 1
Ilse every 3 nights from night 1, Morrow every 4 nights from night 2, watch lasts to night 2010 (Ilse is full on 1, 4, 7, 10…; Morrow on 2, 6, 10…) - case 2
Ilse every 2 nights from night 9, Morrow every 3 nights from night 0, watch lasts to night 309 (night 3 does not count: Ilse is not yet full before night 9) - case 3
Ilse every 4 nights from night 1, Morrow every 6 nights from night 0, watch lasts to night 100-1 (Ilse is only full on odd nights, Morrow only on even ones)
- reward
- 220 XP
- signature
firstDoubleMoon
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- basic coincidence
- watch ends one night too early
- coincidence exactly on the limit
- both full tonight with zero-length watch
- first full night larger than period
- parities never meet
- nightly moons with late starts
- meet on later cycle
- identical moons
- identical moons watch ends before first full
- larger periods