The Ward-Writs of Ostvane Spire
The wizards of Ostvane Spire guard their tower floor by floor. Ward-writs are carved at the foot of the great stair. Each writ admits or turns away the members of one guild on certain floors. The Spire's Warden has posted this contract: for each visitor at the stair, determine which writs hold and give the verdict.
Write wardVerdicts(rules, queries).
rules is a list of strings. Each string is one writ, made of three tokens separated by single spaces: effect guild floors. - effect is grant, bar, or seal. - guild is a lowercase guild name, or * meaning every guild. - floors is one of: - *, meaning every floor; - a single floor n; - an inclusive range a-b with a <= b.
Floors are non-negative integers.
queries is a list of [guild, floor] pairs. Each pair is a lowercase guild name and a non-negative integer floor.
A writ *applies* to a query when both of these hold: - its guild is * or exactly equals the query's guild; - the query's floor lies within its floors.
The verdict for each query is decided as follows: 1. "none" if no writ applies. 2. Otherwise, "seal" if any applicable writ is a seal, whatever the other writs say. 3. Otherwise, consider the applicable grant and bar writs, and keep only the most specific ones: - A writ naming the guild is more specific than a * writ, regardless of floors. - Between writs equal on guild, the one covering fewer floors is more specific. - * floors covers more floors than any range. - A single floor n and the range n-n both cover exactly one floor. 4. If the kept writs include any bar, the verdict is "bar". Otherwise it is "grant".
Return a list of verdict strings, one per query, in the same order as queries. The order of the writs in rules does not affect any verdict.
examples
- case 1
writs "grant * 1-5", "bar thief 3", "seal * 9"; visitors: thief on floor 3, thief on floor 4, cleric on floor 9, cleric on floor 7["bar", "grant", "seal", "none"] - case 2
writs "bar * 4", "grant mage *"; visitor: mage on floor 4["grant"] - the writ naming the mage guild outranks the wildcard writ, even though the wildcard covers fewer floors - case 3
writs "grant ranger 3", "bar ranger 3-3", "grant ranger 2-4"; visitor: ranger on floor 3["bar"] - the two one-floor writs are equally specific, so bar wins
- reward
- 540 XP
- signature
wardVerdicts
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- mixed verdicts
- guild specificity outranks floor narrowness
- equal specificity tie goes to bar
- nested ranges narrowest wins
- seal overrides specific grant
- no writs at all
- no visitors
- guild must match exactly
- multi-digit ranges compare numerically
- guild-specific seal
- wildcard tie loses to named grant