The Pearl-Clam of Saddoran Reef
Commission of the Reefdivers' Lodge, Saddoran Reef - forty silver crowns
Beneath the Saddoran Reef sleeps the Pearl-Clam. It is a shell the size of a longboat, with carved glyphs set into its lip. A diver presses glyphs one at a time. Spell out the clam's secret code and it gapes open on the pearl inside. Press too many wrong glyphs and it clamps shut, ignoring the diver for a while. The Lodge holds a diver's log of one dive and wants to know whether the clam opened, and on which press.
Write openingPress(code, presses, tries, rest):
code: a non-empty string. Its characters are the glyphs of the secret code, in order.presses: a string, possibly empty. Its characters are the glyphs pressed, in order.tries: a positive integer.rest: a non-negative integer.
Glyphs are compared as exact characters.
How the clam behaves. The clam starts awake. While awake, it remembers every glyph pressed since it last woke. The start of the dive counts as waking. It also keeps a count of faults since it last woke, starting at 0. Its *match* is the largest k such that:
- k is smaller than the length of
code, and - the remembered glyphs end with the first k glyphs of
code.
k = 0 always qualifies.
Each press is handled in order, by the first rule that applies:
1. Clamped: the press is ignored. Once it has ignored rest presses since clamping, the clam wakes with its memory empty and its fault count at 0. If rest is 0, it wakes at the moment it clamps and ignores nothing. 2. Awake, and the glyph equals the glyph of `code` at 0-based position k, where k is its current match: the glyph is remembered. If the remembered glyphs now end with the whole of code, the clam opens. 3. Awake, any other glyph: the press is a fault, and the fault count rises by 1. - If the count now equals tries, the clam clamps. Its memory is emptied, and this glyph is not remembered. - Otherwise, the glyph is remembered like any other.
Return the 0-based index into presses of the press on which the clam opens, or -1 if it never opens.
examples
- case 1
code "aab", presses "aaab", tries 3, rest 03. The third "a" is a fault, but the remembered glyphs still end with "aa". The "b" then completes the code. - case 2
code "ab", presses "aaab", tries 2, rest 0-1. The second "a" is fault 1. The third "a" is fault 2, which clamps the clam. That "a" is not remembered, so the clam wakes empty, and the "b" is a new fault. - case 3
code "ab", presses "zzabab", tries 2, rest 25. The two "z" presses clamp the clam. The "a" and "b" at indices 2 and 3 are ignored. The "a" and "b" at indices 4 and 5 open it. - case 4
code "q", presses "pppq", tries 3, rest 1-1. The third "p" clamps the clam, and the "q" is the one press it ignores.
- reward
- 540 XP
- signature
openingPress
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- plain entry after a stray glyph
- overlap survives a fault
- clamping press is forgotten
- presses ignored during rest
- empty log
- single glyph code swallowed by rest
- faults not forgiven by correct glyphs
- faults below the limit still allow opening
- deeper overlap after a fault
- overlap destroyed by clamping
- overlap kept across two faults
- log ends while clamped
- first press opens
- faulting glyph begins a new attempt
- single try means every fault clamps
- zero rest wakes immediately