‹ back to the board

Tuesday, September 15

Apprenticelogic & control flow

The Salt Pans of Bitwick Marsh

The salter out on the marsh keeps one pan of brine and a tally board. Every morning she rewrites the measure in the pan, then chalks the new number on the board beneath the old one.

A day's rewrite works on the measure currently in the pan, and exactly one of three rules applies — the first one that fits, in this order:

1. If the measure is divisible by 3, it becomes the measure divided by 3. 2. Otherwise, if the measure is even, it becomes the measure halved. 3. Otherwise, it becomes the measure multiplied by 3.

After that rule is applied, the tide adds the day's number: on day 1 add 1, on day 2 add 2, and so on. The result is the new measure, and it is the number chalked up for that day.

Write brineLog(start, days), where start is an integer of at least 1 (the measure in the pan before any day has passed) and days is an integer of at least 0 (how many mornings to work through).

Return the whole tally board as a list of integers of length days + 1: the first element is start, and each following element is the measure after that day's rewrite.

  • iterative sequence generation
  • Apprentice

examples

  1. case 1start of 7, days of 3[7, 22, 13, 42] — day 1: 7 is odd and not a third, so 21, plus 1 is 22. Day 2: 22 is even, so 11, plus 2 is 13. Day 3: 13 is odd, so 39, plus 3 is 42.
  2. case 2start of 12, days of 1[12, 5] — 12 is both divisible by 3 and even, but the first matching rule wins: 12 becomes 4, plus 1 is 5.
  3. case 3start of 30, days of 0[30] — no mornings have passed, so the board holds only the starting measure.
reward
190 XP
signature
brineLog
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…
  • six mornings from seven
  • a full fortnight-ish run from nine
  • no mornings at all
  • divisible by three and even at once
  • a single measure to start
  • four mornings from thirty
The Salt Pans of Bitwick Marsh · bitwick