‹ back to the board

Thursday, October 8

Mastermulti-constraint & composite logic

The Delving Writs of Kettleroot Mine

Beneath the hills east of Bitwick, the Delvers' Guild of Kettleroot Mine keeps a book of writs. A writ grants or forbids passage into some part of the galleries. Writs were added over many years and often overlap. A delving party carrying a route needs one verdict before the lift lowers them.

Write writVerdict(rules, path), which returns the verdict for one gallery path.

Arguments

  • path is a string of one or more non-empty segments joined by /, such as "east/vein/seam3". Path segments never contain *.
  • rules is a list of strings, possibly empty. Each string is a verdict word, a single space, then a pattern: "allow <pattern>" or "deny <pattern>".
  • A pattern is one or more non-empty segments joined by /. Each pattern segment is exactly one of these:
  • *, which matches exactly one path segment, whatever it is.
  • **, which matches zero or more consecutive path segments.
  • A literal containing no *, which matches only a path segment equal to it. Comparison is case-sensitive.

A pattern may contain any number of * and ** segments.

Matching

A pattern matches the path when its segments, read in order, can account for every segment of the path, with no path segments left over.

Choosing the verdict

1. Consider only the rules whose pattern matches the path. 2. Among those, keep the rules with the greatest number of literal segments. 3. Among the rules still kept, keep those with the greatest number of * segments. Do not count ** segments here. 4. If any remaining rule is deny, the verdict is deny. Otherwise it is allow. 5. If no rule matches at all, the verdict is deny.

The order of the writs in rules does not affect the result.

Returns the string "allow" or "deny".

  • rule engines with overrides
  • Master

examples

  1. case 1rules ["allow east/**", "deny east/vein/*"], path "east/vein/seam3""deny". Both writs match. The deny writ has 2 literal segments and the allow writ has 1, so the deny writ wins.
  2. case 2rules ["deny **", "allow north/**"], path "north""allow". Here `**` in "north/**" matches zero segments, and that writ has more literal segments than "**".
  3. case 3rules ["allow */gold", "deny deep/*"], path "deep/gold""deny". Both writs have 1 literal segment and 1 `*` segment, so they tie and deny wins.
  4. case 4rules ["allow west/*"], path "west""deny". A `*` must match exactly one segment, so nothing matches.
reward
540 XP
signature
writVerdict
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…
  • more literals beats fewer
  • single star needs exactly one segment
  • double star matches zero segments
  • no match defaults to deny
  • empty rule book
  • full tie goes to deny
  • single star outranks double star
  • literal count dominates star count
  • several double stars
  • all double stars empty
  • order does not matter
  • double star must backtrack
  • trailing segment left over
The Delving Writs of Kettleroot Mine · bitwick