‹ back to the board

Friday, October 30

Apprenticelogic & control flow

The Golem-Brands of Ferrowick Foundry

The Ironwrights' Guild of Ferrowick casts clay-and-iron golems for the frontier: diggers, haulers and sentinels. Before a golem is woken, a brand is stamped into its brow, and the golem obeys whatever that brand says. A misstruck brand can wake a golem that tears its foundry apart. So the Guild pays a standing bounty to any inspector who checks every brand before the waking fires are lit.

Write inspectBrand(brand). It takes a string brand and returns a string verdict.

A well-formed brand has exactly three pieces joined by single dashes (-): a mark, a serial and a command. An example is FER-1236-DIG.

Apply these checks in this order. Return the verdict for the first check that fails:

1. If brand is the empty string, return "empty". 2. Split brand on every - character. Empty pieces count as pieces. If this does not give exactly 3 pieces, return "malformed". 3. The mark (first piece) must be 1 to 3 characters long, and every character must be an uppercase letter A-Z. Otherwise return "bad mark". 4. The serial (second piece) must be exactly 4 characters long, and every character must be a digit 0-9. Otherwise return "bad serial". 5. Add up the serial's first three digits. The last digit of that sum (the sum modulo 10) must equal the serial's fourth digit. Otherwise return "bad check". 6. The command (third piece) must be exactly one of DIG, HAUL, GUARD or HALT. Matching is case-sensitive. Otherwise return "unknown command".

If every check passes, return "ok".

  • input validation branches
  • Apprentice

examples

  1. case 1brand of "FER-1236-DIG""ok" (1 + 2 + 3 = 6, which matches the final digit 6)
  2. case 2brand of "ZZ-4321-SING""bad check" (4 + 3 + 2 = 9, not 1; the check digit is tested before the command, so the unknown command is never reached)
  3. case 3brand of "K-9997-GUARD""ok" (9 + 9 + 9 = 27, whose last digit 7 matches)
  4. case 4brand of "K-1236-""unknown command" (the trailing dash leaves an empty third piece, so there are still three pieces)
reward
220 XP
signature
inspectBrand
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…
  • valid brand
  • empty brand
  • too few pieces
  • too many pieces
  • mark too long
  • lowercase mark
  • empty mark
  • serial with a letter
  • serial too long
  • empty serial from doubled dash
  • wrong check digit
  • check digit wraps modulo 10
  • check digit tested before command
  • lowercase command
  • unknown command
  • trailing dash empty command
  • first failure wins
  • halt command valid
The Golem-Brands of Ferrowick Foundry · bitwick