The Ordinances of the North Gate
The north gate of bitwick charges tolls by ordinance. The wall of the gatehouse has been amended, overwritten and re-amended for two hundred years, and the warden would like the tolls worked out for him.
You are given rules, a list of strings (the ordinances, top to bottom), and carts, a list of strings (the arrivals, in order).
A cart is three lowercase words separated by single spaces: its kind, its origin, its cargo.
A rule is four tokens separated by single spaces: three patterns — one for kind, one for origin, one for cargo — and then an action.
A pattern is one of:
*— matches any word; weight 0!w— an exclamation mark followed by a lowercase word; matches any word other thanw; weight 1w— a lowercase word; matches onlyw; weight 2
A rule matches a cart when all three of its patterns match the corresponding word of that cart. A rule's weight is the sum of its three pattern weights, so 0 to 6.
An action is one of:
=N— a base action: the toll is N==N— a sealed base action: the toll is N+N— a levy: raise the toll by N-N— a levy: lower the toll by N
N is a non-negative integer in decimal.
For each cart, its toll is settled so:
1. Among the rules that match it and carry a base action (sealed or not), exactly one wins. A sealed rule outranks every unsealed rule, whatever their weights. Among rules of equal seal status, the greater weight wins. Among rules of equal seal status and equal weight, the one appearing later in rules wins. The toll starts at the winner's N. If no base rule matches, the toll starts at 0. 2. If the winning rule was sealed, that is the toll, and no levy applies to this cart. 3. Otherwise every matching levy rule is applied to the toll, in the order those rules appear in rules. After each single levy is applied, if the toll is negative it becomes 0.
Return a list of integers: the settled toll for each cart, in the order the carts are given. Either list may be empty.
examples
- case 1
rules of ["cart * salt =10", "* * * +3", "cart brill salt -20", "* * * +2"], carts of ["cart brill salt"][2] — base 10, then +3 makes 13, then -20 would make -7 so it becomes 0, then +2 makes 2 - case 2
rules of ["cart brill wool =9", "* * * ==4", "* * * +5"], carts of ["cart brill wool"][4] — the sealed rule of weight 0 outranks the unsealed rule of weight 6, and no levy follows a sealed base - case 3
rules of ["!rider !brill * =7", "cart * * =3"], carts of ["cart dell wool"][3] — both match and both weigh 2, so the lower one on the wall wins
- reward
- 520 XP
- signature
gateTolls
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- clamping bites in the middle of the wall
- a seal outranks a heavier ordinance
- a bare wall charges nothing
- exclusions weigh less than names
- levies alone, with the floor twice
- no carts came today
- the whole gatehouse at once