The Cellar Ledger of the Sleeping Stoat
The innkeeper of the Sleeping Stoat keeps her cellar in a ledger, and a buyer from the north has sent up a list of what he wants priced.
The ledger, cellar, maps a barrel's name to a record with two keys: "casks", the number of casks of that barrel in the cellar, and "price", the coins one cask of it is worth. Both are non-negative integers.
The buyer's list, wanted, is a list of barrel names (strings). It may be empty, and the same name may appear in it more than once.
Write cellarWorth(cellar, wanted), returning an integer: the sum, over every entry of wanted in order, of that barrel's cask count multiplied by its price. An entry whose name does not appear in the ledger contributes 0. Each appearance of a name in the list contributes its own amount, so a name listed twice is counted twice.
examples
- case 1
cellar with "Barley Ale" (casks 3, price 4) and "Mead" (casks 2, price 7); wanted is ["Barley Ale", "Mead"]26 (12 coins of ale plus 14 of mead) - case 2
cellar with "Mead" (casks 2, price 7); wanted is ["Mead", "Cider", "Mead"]28 (Cider is not in the ledger and adds nothing; Mead counts twice) - case 3
cellar with "Small Beer" (casks 0, price 5); wanted is ["Small Beer"]0
- reward
- 130 XP
- signature
cellarWorth
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- two known barrels
- unknown name contributes nothing
- repeated name counts twice
- empty wanted list
- zero casks is still in the ledger
- empty cellar
- larger order