The Ringing Master's Shorthand
The tower over the inn at bitwick holds a ring of bells, and the ringing master records each night's peal in a shorthand scratched on the beam.
The tower has n bells, numbered 1 to n. Positions in the tower are numbered 1 to n from the left. A row is the order the bells sound in, written as a list of bell numbers; the peal begins at rounds, the row 1, 2, ..., n. Each change in the shorthand turns one row into the next.
Write ringChanges(n, notation), where n is an integer from 1 to 9 and notation is a string. Return a list of rows (a list of lists of integers): rounds first, then one row after each change, in order. Return an empty list if the shorthand is illegal.
Reading the shorthand. Scanning left to right: the character x or the character - is a change on its own; a maximal run of digit characters is one change; . is only a separator and is no change itself. Any character other than 1-9, x, -, . makes the shorthand illegal, as does any . that does not have a change character immediately before it and immediately after it. The empty string is legal and contains no changes.
Working one change. A change names a set of places — positions that hold still. Every position that is not a place swaps with a neighbour.
xand-name no places at all.- A run of digits names those positions as places. The digits must be strictly increasing and each must be at most n, or the shorthand is illegal.
- Implied places. If a change names at least one place: when the number of positions below the lowest named place is odd, position 1 is a place too; when the number of positions above the highest named place is odd, position n is a place too. Neither rule applies to
xor-. - Take the positions that are not places. Each maximal run of consecutive such positions must have even length, or the shorthand is illegal. Within a run the positions pair off in order — first with second, third with fourth, and so on — and the two bells of each pair trade positions.
- A change in which no two bells trade positions is illegal.
If any change is illegal, the whole answer is the empty list.
examples
- case 1
n of 8, notation of "x18"three rows: 1 2 3 4 5 6 7 8, then 2 1 4 3 6 5 8 7, then 2 4 1 6 3 8 5 7 - case 2
n of 6, notation of "4"two rows: 1 2 3 4 5 6, then 1 3 2 4 6 5 — three positions lie below the named place 4, an odd count, so position 1 holds as well, and positions 2-3 and 5-6 trade - case 3
n of 3, notation of "2"the empty list — one position lies below the named place and one above, so positions 1 and 3 both hold, and no bells trade - case 4
n of 5, notation of "" (the empty string)a single row: 1 2 3 4 5
- reward
- 540 XP
- signature
ringChanges
TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.
- a plain course opening on eight
- a place implied at the front
- a place implied at the back
- crosses and dashes mixed
- five bells, five changes
- no cross on odd bells
- the frozen middle place
- an odd gap between places
- every position holding
- digits out of order
- a place beyond the tower
- a stray separator
- silence on a single bell
- two bells cross