AOC 2022-06 (Tuning Trouble)

Mix.install([
  {:kino, "~> 0.7.0"}
])

Common Code

defmodule Day6 do
  def find_unique_prefix(input, length) do
    input
    |> String.graphemes()
    |> Stream.chunk_every(length, 1)
    |> Stream.map(&count_unique_elements/1)
    |> Enum.find_index(&(&1 == length))
    |> Kernel.+(length)
  end

  defp count_unique_elements(list) do
    list
    |> MapSet.new()
    |> MapSet.to_list()
    |> Enum.count()
  end
end

Input

input = Kino.Input.textarea("Input")
input =
  input
  |> Kino.Input.read()

Part 1

Find the index of the first 4 characters that have no repeated characters. 1794

Day6.find_unique_prefix(input, 4)

Part 2

Same thing but with 14 unique characters. 2851

Day6.find_unique_prefix(input, 14)