The Ballast-Stones of the Skyship Ardwenna
Commission of the Ardwenna Aeronauts' Guild, posted at Bitwick
The skyship *Ardwenna* rides the high winds over the Thornreach Peaks, and before each crossing her hold must carry an exact weight of ballast. Too light and she climbs into the storm-shelf; too heavy and she scrapes the summits. The quarry at the mooring-tower cuts ballast stones in only two sizes, and the Guild pays for every stone hauled up the tower, so it wants the load made from as few stones as possible.
Write ballastCount(a, b, target).
ais a positive integer: the weight of one stone of the first size.bis a positive integer: the weight of one stone of the second size.targetis a non-negative integer: the exact total ballast weight required.
You may use any non-negative whole number of stones of each size, with no upper limit on either. The total weight of the chosen stones must equal target exactly.
Return an integer: the smallest possible total number of stones (stones of size a plus stones of size b) whose weights sum to exactly target. If no combination sums to exactly target, return -1. A target of 0 needs no stones, so the answer is 0.
a and b may be equal, and either may be the larger.
examples
- case 1
a of 5, b of 3, target of 93 (three stones of 3; taking one stone of 5 leaves 4, which no number of 3s can make) - case 2
a of 5, b of 3, target of 113 (one stone of 5 and two of 3) - case 3
a of 4, b of 6, target of 7-1 (every combination weighs an even amount) - case 4
a of 4, b of 6, target of 00 (no stones are needed)
- reward
- 220 XP
- signature
ballastCount
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- mixed sizes
- greedy trap
- zero target
- impossible parity
- only small stones work
- sizes swapped
- equal sizes
- tiny first size
- fewer than all-large
- both too heavy
- single stone