Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Day 06

d06.livemd

Day 06

Solution

defmodule D06 do
  def solve(input, distinct) do
    {_chars, first_pos} =
      input
      |> String.graphemes()
      |> Stream.chunk_every(distinct, 1, :discard)
      |> Stream.with_index(1)
      |> Stream.filter(fn {chunk, _start_idx} ->
        MapSet.new(chunk) |> MapSet.size() == distinct
      end)
      |> Enum.take(1)
      |> hd()

    first_pos + distinct - 1
  end
end

Tests

ExUnit.start(autorun: false)

defmodule MyTest do
  use ExUnit.Case, async: true

  def get_input() do
    File.read!("inputs/d06") |> String.trim()
  end

  test "p1" do
    assert D06.solve("mjqjpqmgbljsphdztnvjfqwrcgsmlb", 4) == 7
    assert get_input() |> D06.solve(4) == 1538
  end

  test "p2" do
    assert D06.solve("mjqjpqmgbljsphdztnvjfqwrcgsmlb", 14) == 19
    assert get_input() |> D06.solve(14) == 2315
  end
end

ExUnit.run()