The Glamoured Arch of Fennowyn Hollow
Deep in Fennowyn Hollow stands a mossy arch of white stone. The fey of the Hollow have glamoured it, so it opens only for a traveller who speaks the right number aloud. Each dusk the warden-sprites carve fresh terms into the keystone, and the Thimble Court pays a bounty to any adventurer who can name the number that passes. Too many have guessed wrong and wandered the Hollow until dawn.
The terms are four non-negative integers: lo, hi, total and divisor. The number to be spoken must meet all three of these conditions:
- it is an integer
nwithlo <= n <= hi(both bounds inclusive); - the sum of the decimal digits of
nequalstotal; nis evenly divisible bydivisor.
Write archNumber(lo, hi, total, divisor), which returns the smallest integer meeting all three conditions. If no integer in the range meets them, return -1.
Constraints: 0 <= lo <= hi <= 100000, total >= 0, divisor >= 1. The digit sum of 0 is 0.
examples
- case 1
lo of 37, hi of 100, total of 9, divisor of 472 (45, 54 and 63 have digit sum 9 but are not divisible by 4; 36 would be, but it is below lo) - case 2
lo of 1, hi of 20, total of 10, divisor of 3-1 (only 19 has digit sum 10, and 19 is not divisible by 3) - case 3
lo of 0, hi of 0, total of 0, divisor of 50
- reward
- 220 XP
- signature
archNumber
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- lo bound excludes an earlier match
- first match inside range
- small range with divisor 7
- no number passes
- zero is a valid answer
- single-number range that passes
- lo itself is the answer
- hi itself is the answer
- impossible digit sum
- total zero without zero in range