‹ back to the board

Saturday, September 26

Apprenticelogic & control flow

The Tollhouse at Gallows Ford

The tollhouse at Gallows Ford keeps a day-book of every wagon that crosses. Each entry records three numbers: the wagon's weight in stones, its number of wheels, and a 1 if it carries salt or a 0 if it does not. At dusk the warden needs the day's takings totalled.

Write tollTotal(wagons). wagons is a list of entries, each a list of three integers [weight, wheels, salt], where weight >= 0, wheels >= 1, and salt is either 0 or 1. Return the total toll for every wagon in the list, as an integer.

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

1. If weight is 0, the wagon pays 0 coins and no further rule applies to it. 2. Base: 3 coins if wheels is 2 or fewer, otherwise 5 coins. 3. Weight: add 4 coins if weight is over 100; otherwise add 2 coins if weight is over 40; otherwise add nothing. 4. Salt: if salt is 1 and weight is 100 or less, subtract 3 coins. 5. Floor: if the amount is now less than 1, the wagon pays 1 coin.

An empty list totals 0.

  • nested conditionals
  • Apprentice

examples

  1. case 1one wagon: weight 30, 2 wheels, salt 11 — base 3, no weight surcharge, 3 off for salt would leave 0, so the floor of 1 coin applies
  2. case 2one wagon: weight 120, 4 wheels, salt 19 — base 5 plus 4 for weight over 100, and no salt reduction above 100 stones
  3. case 3two wagons: weight 0 with 4 wheels and salt 1, then weight 60 with 4 wheels and salt 07 — the empty wagon pays nothing at all, the other pays 5 + 2
reward
200 XP
signature
tollTotal
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 day-book
  • salt reduction hits the floor
  • empty wagons pay nothing
  • heavy salt wagon gets no reduction
  • the hundred-stone boundary
  • a full day at the ford
  • six-wheeled ore wagon
The Tollhouse at Gallows Ford · bitwick