01020304050607080910111213141516171819202122232425

Advent of Code

2022/6

Tuning Trouble

in C#

by encse

The preparations are finally complete; you and the Elves leave camp on foot and begin to make your way toward the star fruit grove.

As you move through the dense undergrowth, one of the Elves gives you a handheld device. He says that it has many fancy features, but the most important one to set up right now is the communication system.

Read the full puzzle.

using System;
using System.Linq;

namespace AdventOfCode.Y2022.Day06;

[ProblemName("Tuning Trouble")]
class Solution : Solver {

    public object PartOne(string input) => StartOfBlock(input, 4);
    public object PartTwo(string input) => StartOfBlock(input, 14);

    // Slides a window of length l over the input and finds the first position
    // where each character is different. Returns the right end of the window.
    int StartOfBlock(string input, int l) =>
         Enumerable.Range(l, input.Length)
            .First(i => input.Substring(i - l, l).ToHashSet().Count == l);
}

Please ☆ my repo if you like it!

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