‹ back to the board

Thursday, September 17

Apprenticelogic & control flow

The Tollhouse at Mirebridge

The tollkeeper at Mirebridge has gone to market and left the gate to you. A queue of wagons waits on the near bank, and the toll board on the wall lists the rates.

You are given wagons, a list of wagons. Each wagon is a list of three values: the number of wheels (an integer, zero or more), the weight in stone (an integer, zero or more), and the cargo (a string).

The toll for one wagon is worked out in this order:

1. Base rate, by wheels. Exactly 2 wheels costs 3 coins. Exactly 4 wheels costs 5 coins. Any other number of wheels costs 8 coins. 2. Weight surcharge. If the weight is more than 500, add 4 coins. Otherwise, if the weight is more than 200, add 2 coins. Otherwise add nothing. 3. Cargo adjustment. If the cargo is exactly "grain", the toll so far is halved and rounded down to a whole number of coins. If the cargo is exactly "ore", the toll so far is doubled. Any other cargo leaves the toll unchanged. 4. Minimum toll. If the result is less than 2 coins, the wagon pays 2 coins instead.

Write mirebridgeToll(wagons), which returns an integer: the sum of the tolls of every wagon in the list. An empty list returns 0.

  • nested conditionals
  • Apprentice

examples

  1. case 1wagons of [[2, 150, "wool"]]3 — a two-wheeled cart is 3 coins, 150 stone adds nothing, wool changes nothing
  2. case 2wagons of [[2, 80, "grain"]]2 — 3 coins halved and rounded down is 1, which is below the minimum, so the wagon pays 2
  3. case 3wagons of [[4, 500, "grain"], [6, 600, "ore"]]27 — the first is 5 + 2 = 7, halved down to 3; the second is 8 + 4 = 12, doubled to 24
  4. case 4wagons of [[3, 200, "ore"]]16 — three wheels is the 8-coin rate, 200 stone is not more than 200 so nothing is added, ore doubles it
reward
190 XP
signature
mirebridgeToll
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…
  • empty queue
  • plain two-wheeled cart
  • grain falls to the minimum
  • weight boundaries exactly
  • odd wheel counts take the high rate
  • the whole morning queue
The Tollhouse at Mirebridge · bitwick