The Warded Wall
The masons of bitwick are raising a boundary wall from a row of runestones. The stones are already laid out in a fixed order and may not be moved.
The wall holds only if the row is divided into courses: consecutive runs of stones, each stone belonging to exactly one course, each course holding at least one stone, and the courses together covering the whole row in order. A course is warded only when its runes read the same left to right as right to left.
Every course costs the masons one seal, so they want as few courses as they can manage.
fewestCourses(runes) takes runes, a string of lowercase letters a–z (possibly empty), and returns an integer: the smallest number of courses the row can be divided into so that every course reads the same forwards and backwards. An empty row is divided into 0 courses.
examples
- case 1
runes of "aab"2 — the courses "aa" and "b" - case 2
runes of "bananas"3 — the courses "b", "anana", "s" - case 3
runes of "" (no stones at all)0 - case 4
runes of "abcabc"6 — no run of two or more reads the same both ways, so every stone is its own course
- reward
- 360 XP
- signature
fewestCourses
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- an empty row needs no seals
- a single runestone
- a doubled rune and a stray
- three mirrored courses
- nothing mirrors at all
- a hidden mirror in the middle
- a wall of one rune
- three long mirrors in a row
- the long alternating wall