‹ back to the board

Tuesday, October 20

Apprenticelogic & control flow

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).

  • a is a positive integer: the weight of one stone of the first size.
  • b is a positive integer: the weight of one stone of the second size.
  • target is 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.

  • bounded search
  • Apprentice

examples

  1. case 1a 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)
  2. case 2a of 5, b of 3, target of 113 (one stone of 5 and two of 3)
  3. case 3a of 4, b of 6, target of 7-1 (every combination weighs an even amount)
  4. case 4a of 4, b of 6, target of 00 (no stones are needed)
reward
220 XP
signature
ballastCount
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…
  • 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
The Ballast-Stones of the Skyship Ardwenna · bitwick