‹ back to the board

Sunday, October 11

Apprenticelogic & control flow

The Counting Rook of Skelderwick Cairn

On the windswept summit of Skelderwick Cairn a grey rook perches on the capstone of a sealed barrow-tomb. Every dusk it caws a single whole number, and every dusk the number follows a rule. The Cairnwatch Company will pay a bounty to whoever can foretell the rook's calls in advance, so its warders can be ready at the tomb's stone door.

The rule: - The rook's first call (position 0) is the number first. - Every later call depends only on the call made just before it. Say the previous call was the number v, made at position i. - If v was also called at some position before i, the new call is i - j, where j is the most recent such earlier position. - If v was never called before position i, the new call is 0.

Write rookCalls(first, n).

Arguments: - first: a non-negative integer, 0 <= first <= 1000, the rook's first call. - n: a non-negative integer, 0 <= n <= 1000, how many calls to foretell.

Returns: a list of exactly n integers: the rook's calls at positions 0 through n - 1, in order. If n is 0, return an empty list.

  • iterative sequence generation
  • Apprentice

examples

  1. case 1first of 0, n of 10[0, 0, 1, 0, 2, 0, 2, 2, 1, 6]
  2. case 2first of 1, n of 6[1, 0, 0, 1, 3, 0] (the 1 at position 3 was last called at position 0, so the next call is 3)
  3. case 3first of 7, n of 0[]
reward
200 XP
signature
rookCalls
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…
  • starts at zero, twenty calls
  • first call is remembered
  • starts at three
  • starts at five, uses most recent position
  • no calls at all
  • single call
  • two calls
The Counting Rook of Skelderwick Cairn · bitwick