01020304050607080910111213141516171819202122232425

Advent of Code

2015/10

Elves Look, Elves Say

in C#

by encse

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).

Read the full puzzle.

using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AdventOfCode.Y2015.Day10;

[ProblemName("Elves Look, Elves Say")]
class Solution : Solver {

    public object PartOne(string input) => LookAndSay(input).Skip(39).First().Length;
    public object PartTwo(string input) => LookAndSay(input).Skip(49).First().Length;

    IEnumerable<string> LookAndSay(string input) {
        while (true) {
            var sb = new StringBuilder();
            var ich = 0;
            while (ich < input.Length) {
                if (ich < input.Length - 2 && input[ich] == input[ich + 1] && input[ich] == input[ich + 2]) {
                    sb.Append("3");
                    sb.Append(input[ich]);
                    ich += 3;
                } else if (ich < input.Length - 1 && input[ich] == input[ich + 1]) {
                    sb.Append("2");
                    sb.Append(input[ich]);
                    ich += 2;
                } else {
                    sb.Append("1");
                    sb.Append(input[ich]);
                    ich += 1;
                }
            }
            input = sb.ToString();
            yield return input;
        }
    }
}

Please ☆ my repo if you like it!

© 2025 Advent of Code is a registered trademark in the US Images provided by Bing image creator