Powered by AppSignal & Oban Pro

Day 06

2022/day06.livemd

Day 06

Section

defmodule Day06 do
  def part1(input_path) do
    solve(input_path, 4)
  end

  def part2(input_path) do
    solve(input_path, 14)
  end

  defp solve(input_path, chunk_size) do
    input_path
    |> File.stream!([], 1)
    |> Stream.chunk_every(chunk_size, 1, :discard)
    |> Stream.map(&IO.inspect(&1, label: "CHUNK"))
    |> Stream.map(&Enum.uniq/1)
    |> Stream.map(&IO.inspect(&1, label: "UNIQ"))
    |> Stream.map(&length/1)
    |> Stream.map(&IO.inspect(&1, label: "LENGTH"))
    |> Enum.find_index(&(&1 == chunk_size))
    |> Kernel.+(chunk_size)
  end
end
input_path = "#{__DIR__}/day06.txt"
Day06.part1(input_path)
Day06.part2(input_path)