The Sealed Vault of Hollowmere
By commission of the Wardens' Guild of Bitwick.
Beneath the ruined keep of Hollowmere lies a vault sealed by a rune lock. The door answers to one fixed sequence of runes, its *code*, pressed one rune at a time. The Guild has recovered the code and the exact runes a failed expedition pressed. It pays for a faithful reckoning of every moment the door stood open.
Write runeLock(code, presses).
codeis a non-empty string of uppercase letters. Each letter is one rune.pressesis a string of uppercase letters, possibly empty. These are the runes pressed, in order.
The lock tracks four whole numbers, all starting at 0: progress, strikes, silence and lockouts.
Handle the presses in order. For the press at 0-based index i with rune r:
1. If silence is greater than 0, reduce silence by 1. The press has no other effect. 2. Otherwise, if `r` equals the rune at 0-based position progress in `code`, increase progress by 1. If progress now equals the length of code, the door opens at index i. Record i, then set progress to 0 and strikes to 0. 3. Otherwise the press is a strike. Increase strikes by 1. - If strikes is now 3, the lock seals itself. Increase lockouts by 1, set silence equal to the new value of lockouts, and set strikes to 0 and progress to 0. - If strikes is not 3, set progress to 1 if r equals the first rune of code, and to 0 otherwise.
A correct press never changes strikes. Only an opening or a lockout clears it. Lockouts is never reset.
Return a list of every index at which the door opened, in increasing order. Return an empty list if it never opened.
examples
- case 1
code "ABC", presses "ABABC"[4]. The wrong A at index 2 is a strike, but it is also the code's first rune, so it begins a fresh attempt that finishes at index 4. - case 2
code "AAB", presses "AAAB"[]. The third A is a strike that leaves progress at 1. The B then fails against the code's second rune, an A. - case 3
code "AB", presses "XXABXAB"[3, 6]. Opening at index 3 clears the two strikes, so the X at index 4 is only the first strike. - case 4
code "AB", presses "XXXQXXXQAB"[]. The first lockout swallows Q. The second lockout swallows two presses, Q and A, and then B is a strike.
- reward
- 540 XP
- signature
runeLock
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- plain opening
- no presses
- wrong first rune restarts attempt
- lock never looks back
- restart then complete
- first lockout swallows one press
- open after first lockout
- correct presses do not clear strikes
- second lockout swallows two
- opens twice after growing lockouts
- opening clears strikes
- lockouts persist across openings
- single rune code
- lockout clears progress even on first rune