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:
sacksis a non-negative integer: the number of sacks waiting on the dock.capis an integer of 1 or more: the most sacks one barge can carry.feeis 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.
examples
- case 1
sacks of 17, cap of 5, fee of 312 — four barges leave (the fourth carries only 2 sacks), and 4 x 3 = 12 coins - case 2
sacks of 20, cap of 5, fee of 312 — four full barges, no fifth trip needed - case 3
sacks of 0, cap of 5, fee of 30 — no grain, no barge, no coins
- reward
- 130 XP
- signature
ferryToll
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