Skip to the content.

Day 10: Elves Look, Elves Say

Part 1

Today, the Elves are playing a game called look-and-say. They take turns making sequences by reading aloud the previous sequence and using that reading as the next sequence. For example, 211 is read as "one two, two ones", which becomes 1221 (1 2, 2 1s).

Look-and-say sequences are generated iteratively, using the previous value as input for the next step. For each step, take the previous value, and replace each run of digits (like 111) with the number of digits (3) followed by the digit itself (1).

For example:

Starting with the digits in your puzzle input, apply this process 40 times.

Task 1: What is the length of the result?

val puzzle = "3113322113"
val n = 40
var number = puzzle.first()
var count = 0

var sequence = puzzle
var seq : StringBuilder = StringBuilder("")

for (i in 0 until n) {
    number = sequence.first()
    count = 0
    for (c in sequence) {
        if (c == number) {
            count++
        } else {
            seq.append(count)
            seq.append(number)
            number = c
            count = 1
        }
    }
    seq.append(count)
    seq.append(number)
    sequence = seq.toString()
    seq = seq.deleteRange(0,seq.length)
    
    print("${i+1}:\t${sequence.length}\n")
}
sequence.length
329356

Part 2

Neat, right? You might also enjoy hearing John Conway talking about this sequence (that’s Conway of Conway’s Game of Life fame).

Now, starting again with the digits in your puzzle input, apply this process 50 times.

Task 2: What is the length of the new result?

val n = 50
sequence.length
4666278