27
1
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.
7Can we use any falsey value instead of just
-1
? – mbomb007 – 2017-06-28T20:05:54.717Can we know for certain that all of the list entries in the input will be positive? – Post Rock Garf Hunter – 2017-06-28T20:58:19.063
@WheatWizard Did you read all the test cases? – Okx – 2017-06-28T20:58:48.690
@Okx Ah I missed the -2 there. Rather unfortunate costs me a ton of bytes. – Post Rock Garf Hunter – 2017-06-28T20:59:50.503
@Okx Haskell, my answer is below. – Post Rock Garf Hunter – 2017-06-28T21:00:30.143
1What do you mean by "NOT a working prefix of the Kolakoski sequence"? I had assumed you meant the list does not eventually reach
[2]
until I saw the[2,2,1,1,2,1,2]
test case. – ngenisis – 2017-06-28T21:42:54.9231@ngenisis It will eventually reach two without any intermediate step having any elements other than
1
and2
. – Post Rock Garf Hunter – 2017-06-29T00:27:35.1472Might be a good idea to add
[1]
as a test case. – Emigna – 2017-06-29T09:54:23.087Will truthy test cases always start with a
1
? – scatter – 2017-06-29T14:13:55.880@Christian: No. For example
[2]
,[2,2]
and[2,2,1]
are all truthy. – Emigna – 2017-06-29T14:38:41.7501@mbomb007 any distinct value is fine. A positive integer is not fine. If you're 1-indexing 0 is fine. "False" is fine. Erroring is fine. Any non-positive return value is fine, even -129.42910. – Magic Octopus Urn – 2017-06-29T15:33:54.807
@MagicOctopusUrn If the falsy value isn't specific but can vary (except from being a positive integer, e.g. be a list), would it be valid? – Erik the Outgolfer – 2017-09-03T17:58:06.447
@EriktheOutgolfer I'd need an example – Magic Octopus Urn – 2017-09-03T21:16:09.600
@MagicOctopusUrn For example, being able to return any inconsistent list for falsy (e.g.
[3, 4]
,[7, -100, 8.5]
,[1, 2, 2, 1]
etc.) and a positive number for truthy. – Erik the Outgolfer – 2017-09-04T07:27:44.353@EriktheOutgolfer yeah, that should be fine, as long as the truthy value is consistent and you define all other outputs as falsy. – Magic Octopus Urn – 2017-09-05T14:01:30.743
@MagicOctopusUrn Alright, now my answer returns a list for falsy inputs and a positive integer for truthy ones. – Erik the Outgolfer – 2017-09-05T14:29:54.703
I think that the empty list is an unnecessary complication. You state:
given a list of integers
. Nothing is not an integer. Please consider removing the fourth falsy scenario. – Andreï Kostyrka – 2018-03-23T17:15:45.043