The Warden of the Low Bridge
The warden of the Low Bridge will not raise the chain for a cart whose permit stamp is wrong, and the town clerk wants the rejections written down by reason rather than argued about at the water's edge.
A permit stamp is a single string, code. The warden checks five rules in this exact order:
1. "length" — code must be exactly 9 characters long. 2. "mark" — the character at index 3 must be a hyphen, -. 3. "house" — the characters at indices 0, 1 and 2 must each be an uppercase letter A–Z. 4. "number" — the characters at indices 4 through 8 must each be a digit 0–9. 5. "seal" — reading those five digits in order, the fifth digit must equal the sum of the first four digits modulo 10.
Write checkPermit(code), which takes a string and returns a string: the name of the first rule in the list above that the stamp fails ("length", "mark", "house", "number" or "seal"), or "valid" if it fails none of them.
A stamp may break several rules; only the earliest one in the list is reported. code may be any string, including the empty string.
examples
- case 1
code of "BRG-41230""valid" — nine characters, hyphen in place, three capitals, five digits, and 4+1+2+3 = 10, which is 0 modulo 10, matching the final digit - case 2
code of "BR9-00000""house" — every other rule passes, but index 2 is not an uppercase letter - case 3
code of "BRG-4123""length" — only eight characters - case 4
code of "BRG-99990""seal" — 9+9+9+9 = 36, which is 6 modulo 10, but the final digit is 0
- reward
- 220 XP
- signature
checkPermit
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- a clean stamp passes
- all zeroes seal correctly
- empty stamp is a length failure
- too short, even though the hyphen is missing too
- right length, wrong mark
- lowercase house letters
- a digit among the house letters outranks the seal
- a letter among the number digits
- seal does not match
- seal wraps past ten
- too long by one character