‹ back to the board

Saturday, October 24

Apprenticelogic & control flow

The Beacon-Codes of Ostrel Point

Along the black cliffs of Ostrel Point, the Keepers' Guild flashes coded warnings from tower to tower whenever raider sails are sighted. A malformed code can send a whole fleet onto the rocks. The Guild posts a bounty for a checker that inspects each code before it is lit and names exactly what is wrong with it.

Write checkBeacon(code), where code is a string. Return a string: the label of the first rule below that code breaks, checking the rules in the order listed. If it breaks none of them, return "valid".

1. "empty": the code is the empty string. 2. "length": the code is shorter than 4 characters or longer than 12 characters. 3. "symbol": the code contains any character that is not an uppercase ASCII letter A-Z or an ASCII digit 0-9. Lowercase letters, spaces and punctuation all break this rule. 4. "start": the first character of the code is not a letter. 5. "digit": the code contains no digit at all. 6. "repeat": the same character appears three or more times consecutively, in adjacent positions.

A code may break several rules at once. Only the earliest broken rule in this list is reported.

  • input validation branches
  • Apprentice

examples

  1. case 1code of "KX7R""valid"
  2. case 2code of "ab" (too short, and lowercase)"length": the length rule comes before the symbol rule
  3. case 3code of "1234" (starts with a digit, and has digits)"start"
  4. case 4code of "ZZZ9""repeat": three Z's in a row
reward
220 XP
signature
checkBeacon
not yet run

TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.

loading the editor…
  • empty code
  • too short
  • too long at thirteen
  • minimum length valid
  • maximum length valid
  • hyphen is a symbol
  • lowercase is a symbol
  • space is a symbol
  • length before symbol
  • symbol before start
  • starts with digit
  • start before digit rule
  • no digit
  • digit before repeat
  • letter repeat
  • digit repeat in middle
  • repeat at end
  • pairs are fine
  • non-adjacent repeats are fine
  • ordinary valid code