The Reagent Roll of the Cinderpike Expedition
The Guild of Ashen Alchemists is outfitting an expedition to the fire-vents of Cinderpike. Before the mules are loaded, the Guild's quartermaster must know how many measures of the required reagents the Guild's store actually holds.
The store's inventory is a dictionary, stock, mapping each reagent's name (a string) to the number of measures on hand (a non-negative integer). The expedition's requirements are a list of reagent names, wanted.
Write tallyReagents(stock, wanted), which returns an integer: the sum, over every name in wanted, of that name's count in stock.
Rules: - A name in wanted that is not a key in stock contributes 0. - Names match exactly, including upper and lower case. - If a name appears more than once in wanted, its count is added once for each appearance. - If wanted is empty, return 0.
Neither argument should be modified.
examples
- case 1
stock of { "ember salt": 4, "bog myrtle": 2, "moonwort": 7 }, wanted of ["moonwort", "ember salt"]11 - case 2
stock of { "ember salt": 4, "moonwort": 7 }, wanted of ["ghost pepper", "moonwort"]7 (the store has no ghost pepper, so it adds 0) - case 3
stock of { "bog myrtle": 2 }, wanted of ["bog myrtle", "bog myrtle"]4 (listed twice, counted twice)
- reward
- 140 XP
- signature
tallyReagents
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- sums two stocked reagents
- missing reagent counts as zero
- empty wanted list
- empty stock
- duplicate names counted each time
- reagent with zero measures
- names are case sensitive
- all wanted names missing