‹ back to the board

Friday, October 23

Adeptalgorithms & data structures

The Briar-Hymns of Quessaly Wood

Commission of the Greenward Circle — posted at the Bitwick inn

A wall of living briar rings Quessaly Wood. It parts only for a traveller who sings the Long Chant, and the dryads of the Greenward Circle accept the chant only when it is sung as an unbroken run of their sanctioned hymn-phrases, one after another, with nothing left over. Before the Circle sends its next expedition through, it wants a reckoning: how many distinct ways can the chant be divided into sanctioned phrases?

The contract

Write chantWays(chant, verses).

  • chant is a string of lowercase letters a–z. It may be empty and is at most 1000 characters long.
  • verses is a list of at most 100 non-empty strings of lowercase letters: the sanctioned phrases. It may be empty. It may contain the same phrase more than once; a repeated phrase is still only one phrase.

Return an integer: the number of distinct sequences of phrases, each taken from verses, whose concatenation in order is exactly chant. A phrase may be used any number of times in a sequence. Two sequences are different if they differ in length or in the phrase at any position.

The empty chant is produced by exactly one sequence, the empty one, so its count is 1. If no sequence produces the chant, return 0.

Inputs are chosen so that the answer is less than 2^53.

  • dynamic programming
  • Adept
  • efficiency bonus

examples

  1. case 1chant of "thornrose", verses of ["thorn", "rose", "thornrose", "or", "th", "nrose"]3 — thorn|rose, thornrose, and th|or|nrose
  2. case 2chant of "", verses of ["ivy"]1 — the empty chant is sung one way: by singing no phrases
  3. case 3chant of "aaa", verses of ["a", "aa", "a"]3 — a|a|a, a|aa, aa|a (the repeated "a" is still one phrase)
  4. case 4chant of "briar", verses of ["bri", "iar", "b"]0 — no sequence of phrases spells it exactly
reward
360 XP
signature
chantWays
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…
  • thornrose three splits
  • empty chant counts once
  • no possible split
  • repeated verse counts once
  • single verse is whole chant
  • no verses at all
  • overlapping phrases
  • long chant of ones and twos
  • long chant of ones twos and threes
The Briar-Hymns of Quessaly Wood · bitwick