The Kolakoski sequence is an infinite sequence of natural numbers, (excluding zero); with the property that:

 if you form a new sequence from the counts of runs of the same number in the first sequence, this new sequence is the same as the first sequence.
Example

This is not a Kolakoski sequence:

1,1,2,2,2,1,2,2,1,2,...
Its sequence of run counts, (sometimes called a run length encoding, (RLE); but a true RLE also gives the character that each run encodes), is calculated like this:

 Starting from the leftmost number of the sequence we have 2 ones, followed by 3 twos, then 1 ones, 2 twos, 1 one, ...
The above gives the RLE of:

2, 3, 1, 2, 1, ...
The original sequence is different from its RLE in this case. It would be the same for a true Kolakoski sequence.

Creating a Kolakoski sequence

Lets start with the two numbers (1, 2) that we will cycle through; i.e. they will be used in this order:
 1,2,1,2,1,2,....

 We start the sequence s with the first item from the cycle c:
 1
 An index, k, into the, (expanding), sequence will step, or index through each item of the sequence s from the first, at its own rate.
 
We will arrange that the k'th item of s states how many times the last item of sshould appear at the end of s.

We started s with 1 and therefore s[k] states that it should appear only the 1 time.



Increment k
Get the next item from c and append it to the end of sequence s. s will then become:
 1, 2
k was moved to the second item in the list and s[k] states that it should appear two times, so append another of the last item to the sequence s:
 1, 2,2
Increment k
Append the next item from the cycle to the list:
 1, 2,2, 1
k is now at the third item in the list that states that the last item should appear twice so add another copy of the last item to the sequence s:
 1, 2,2, 1,1
increment k
...

Note that the RLE of 1, 2, 2, 1, 1, ... begins 1, 2, 2 which is the beginning of the original sequence. The generation algorithm ensures that this will always be the case.

Task

 Create a routine/proceedure/function/... that given an initial ordered list/array/tuple etc of the natural numbers (1, 2), returns the next number from the list when accessed in a cycle.
 Create another routine that when given the initial ordered list (1, 2) and the minimum length of the sequence to generate; uses the first routine and the algorithm above, to generate at least the requested first members of the kolakoski sequence.
 Create a routine that when given a sequence, creates the run length encoding of that sequence (as defined above) and returns the result of checking if sequence starts with the exact members of its RLE. (But note, due to sampling, do not compare the last member of the RLE).
 Show, on this page, (compactly), the first 20 members of the sequence generated from (1, 2)
 Check the sequence againt its RLE.
 Show, on this page, the first 20 members of the sequence generated from (2, 1)
 Check the sequence againt its RLE.
 Show, on this page, the first 30 members of the Kolakoski sequence generated from (1, 3, 1, 2)
 Check the sequence againt its RLE.
 Show, on this page, the first 30 members of the Kolakoski sequence generated from (1, 3, 2, 1)
 Check the sequence againt its RLE.
(There are rules on generating Kolakoski sequences from this method that are broken by the last example)

def create_generator(ar)
  Enumerator.new do |y|
    cycle = ar.cycle
    s = []
    loop do
      t = cycle.next
      s.push(t)
      v = s.shift
      y << v
      (v-1).times{s.push(t)}
    end
  end
end
 
def rle(ar)
  ar.slice_when{|a,b| a != b}.map(&:size)
end
 
[[20, [1,2]], 
 [20, [2,1]], 
 [30, [1,3,1,2]],
 [30, [1,3,2,1]]].each do |num,ar|
  puts "\nFirst #{num} of the sequence generated by #{ar.inspect}:"
  p res = create_generator(ar).take(num)
  puts "Possible Kolakoski sequence? #{res.join.start_with?(rle(res).join)}"
end