Overview
Some of you might be aware of the Kolakoski Sequence (A000002), a well know self-referential sequence that has the following property:
It is a sequence containing only 1's and 2's, and for each group of 1's and twos, if you add up the length of runs, it equals itself, only half the length. In other words, the Kolakoski sequence describes the length of runs in the sequence itself. It is the only sequence that does this except for the same sequence with the initial 1 deleted. (This is only true if you limit yourself to sequences made up of 1s and 2s - Martin Ender)
The Challenge
The challenge is, given a list of integers:
- Output
-1
if the list is NOT a working prefix of the Kolakoski sequence. - Output the number of iterations before the sequence becomes
[2]
.
The Worked Out Example
Using the provided image as an example:
[1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1] # Iteration 0 (the input).
[1,2,2,1,1,2,1,2,2,1,2] # Iteration 1.
[1,2,2,1,1,2,1,1] # Iteration 2.
[1,2,2,1,2] # Iteration 3.
[1,2,1,1] # Iteration 4.
[1,1,2] # Iteration 5.
[2,1] # Iteration 6.
[1,1] # Iteration 7.
[2] # Iteration 8.
Therefore, the resultant number is 8
for an input of [1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1]
.
9
is also fine if you are 1-indexing.
The Test Suite (You can test with sub-iterations too)
------------------------------------------+---------
Truthy Scenarios | Output
------------------------------------------+---------
[1,1] | 1 or 2
[1,2,2,1,1,2,1,2,2,1] | 6 or 7
[1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1] | 8 or 9
[1,2] | 2 or 3
------------------------------------------+---------
Falsy Scenarios | Output
------------------------------------------+---------
[4,2,-2,1,0,3928,102904] | -1 or a unique falsy output.
[1,1,1] | -1
[2,2,1,1,2,1,2] (Results in [2,3] @ i3) | -1 (Trickiest example)
[] | -1
[1] | -1
If you're confused:
Truthy: It will eventually reach two without any intermediate step having any elements other than 1
and 2
. – Einkorn Enchanter 20 hours ago
Falsy: Ending value is not [2]
. Intermediate terms contain something other than something of the set [1,2]
. A couple other things, see examples.
This is code-golf, lowest byte-count will be the victor.
-1
? \$\endgroup\$[2]
until I saw the[2,2,1,1,2,1,2]
test case. \$\endgroup\$1
and2
. \$\endgroup\$[1]
as a test case. \$\endgroup\$