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

Advent of code day 06

2022/livebooks/day-06.livemd

Advent of code day 06

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

Setup input

input = Kino.Input.textarea("Please paste your input file:")
defmodule Recursion do
  def solve(enum, amount_of_distinct_chars) do
    recur(enum, amount_of_distinct_chars, amount_of_distinct_chars)
  end

  def recur([], _final, counter), do: nil

  def recur(enum, final, counter) do
    {half, remain} = Enum.split(enum, final)
    [_head | tail] = half
    is_repeated = Enum.frequencies(half) |> Enum.find(fn {_key, value} -> value >= 2 end)

    case is_repeated do
      nil -> counter
      _ -> recur(tail ++ remain, final, counter + 1)
    end
  end
end
input =
  input
  |> Kino.Input.read()
  |> String.graphemes()

Part 01

Recursion.solve(input, 4)

Part 02

Recursion.solve(input, 14)