‹ back to the board

Wednesday, September 9

Apprenticelogic & control flow

The Toll Gate of Wyrmback Bridge

A notice, pinned by the door of the Wick & Whistle inn.

The toll-keeper at Wyrmback Bridge has lost her tally-clerk to the harvest, and the morning caravan is already queued on the far bank. She needs someone to total up a day's takings.

You are given carts, a list of carts in the order they reach the gate. Each cart is a 3-element list [kind, weight, wheels], where kind is a string, weight is a positive integer (the load in stones), and wheels is a positive integer.

The posted toll for a single cart, in coins:

  • If kind is "grain": nothing at all when weight is 100 or less; otherwise 1 + (weight - 100) // 50 coins, using floor division.
  • If kind is "stone": 5 coins, plus 10 more if weight is greater than 300, or else plus 4 more if weight is greater than 150.
  • If kind is "cloth": 4 coins if weight is greater than 200, and 2 coins otherwise.
  • Any other value of kind is not on the posted list: that cart is waved through for 0 coins, and no further charge of any sort applies to it.

Then, for every cart that is on the posted list and whose kind is not "grain", add 3 coins if wheels is greater than 4.

Write bridgeToll(carts), which returns the sum of the tolls of all the carts as an integer. An empty list of carts returns 0.

  • nested conditionals
  • Apprentice

examples

  1. case 1carts of [["stone", 151, 4]]9 — 5 base, plus 4 because the weight passes 150; only 4 wheels, so no wheel charge
  2. case 2carts of [["grain", 250, 6]]4 — 1 + (250 - 100) // 50 = 4, and grain never pays the wheel charge whatever the count
  3. case 3carts of [["ash", 40, 2], ["cloth", 201, 5]]7 — ash is not on the posted list and pays nothing; the cloth pays 4 plus 3 for its fifth wheel
reward
200 XP
signature
bridgeToll
not yet run

TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.

loading the editor…
  • no carts at the gate
  • grain bands and grain's wheel exemption
  • stone weight bands
  • cloth boundary at two hundred stones
  • a fifth wheel on a light stone cart
  • unlisted cargo is waved through
  • the whole morning caravan
The Toll Gate of Wyrmback Bridge · bitwick