Powered by AppSignal & Oban Pro

Day 6

2022/day_06.livemd

Day 6

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

Common

defmodule Common do
  def parse_input(textarea) do
    textarea
    |> Kino.Input.read()
  end

  def index_of_first_unique_chunk(datastream_buffer, chunk_size) do
    datastream_buffer
    |> String.graphemes()
    |> Enum.chunk_every(chunk_size, 1)
    |> Enum.find_index(&unique_chunk?/1)
    |> Kernel.+(chunk_size)
  end

  defp unique_chunk?(chunk) do
    Enum.count(MapSet.new(chunk)) == Enum.count(chunk)
  end
end

Input

textarea =
  Kino.Input.textarea("Input:",
    default: """
    mjqjpqmgbljsphdztnvjfqwrcgsmlb
    """
  )
input = Common.parse_input(textarea)

Part 1

defmodule Part1 do
  def run(datastream_buffer) do
    Common.index_of_first_unique_chunk(datastream_buffer, 4)
  end
end
Part1.run(input)

Part 2

defmodule Part2 do
  def run(datastream_buffer) do
    Common.index_of_first_unique_chunk(datastream_buffer, 14)
  end
end
Part2.run(input)