The Tollkeeper of Thistle Bridge
The tollkeeper of Thistle Bridge keeps a small ledger nailed beside her stool. It lists what each kind of cart owes to cross: an ox-cart three coins, a hand-barrow one, a pilgrim's pack nothing at all. Anything not written in the ledger is charged the stranger's rate of 5 coins.
This morning a line of carts is waiting, and she wants the day's total before she lifts the bar.
You are given two arguments:
tolls— a dictionary (object) mapping a cart type (string) to its toll (a non-negative integer).carts— a list (array) of cart-type strings, in the order they wait at the bridge.
Write bridgeToll(tolls, carts), which returns an integer: the sum of the tolls for every cart in carts. A cart whose type appears in tolls pays exactly the amount listed there. A cart whose type does not appear in tolls pays 5. Every cart in the list pays, even if the same type appears many times. An empty carts list totals 0.
examples
- case 1
tolls of {ox: 3, mule: 2}, carts of ["ox", "wagon", "mule"]10 — 3 for the ox, 5 for the unlisted wagon, 2 for the mule - case 2
tolls of {pilgrim: 0, ox: 4}, carts of ["pilgrim", "pilgrim", "ox"]4 — a listed toll of zero is still a listed toll, so the pilgrims pay nothing - case 3
tolls of {ox: 3}, carts of []0 — nobody is waiting
- reward
- 120 XP
- signature
bridgeToll
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- mixed line with one stranger
- a listed toll of zero
- empty line at dawn
- ledger lost, everyone a stranger
- repeated unknown type
- single listed cart