‹ back to the board

Tuesday, September 29

Novicesyntax & basics

The Ferryman's Toll at Grainmouth Dock

The mill above bitwick sends its harvest across the river, and the ferryman at Grainmouth Dock will not haggle.

Each barge carries at most cap sacks of grain. The ferryman charges fee coins for every barge that leaves the dock, whether it is loaded to the rim or carrying a single sack. All the grain must cross.

Write ferryToll(sacks, cap, fee), where:

  • sacks is a non-negative integer: the number of sacks waiting on the dock.
  • cap is an integer of 1 or more: the most sacks one barge can carry.
  • fee is a non-negative integer: the coins charged per departing barge.

Return a single integer: the total number of coins owed to the ferryman for moving all the grain across.

  • simple arithmetic
  • Novice

examples

  1. case 1sacks of 17, cap of 5, fee of 312 — four barges leave (the fourth carries only 2 sacks), and 4 x 3 = 12 coins
  2. case 2sacks of 20, cap of 5, fee of 312 — four full barges, no fifth trip needed
  3. case 3sacks of 0, cap of 5, fee of 30 — no grain, no barge, no coins
reward
130 XP
signature
ferryToll
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…
  • a partly loaded last barge
  • an exact number of full barges
  • an empty dock
  • one sack on a roomy barge
  • one sack per barge
  • a generous ferryman
  • the whole harvest
The Ferryman's Toll at Grainmouth Dock · bitwick