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

2022 day - 4

2022/elixir/day-4.livemd

2022 day - 4

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

Section

input = Kino.Input.textarea("")
defmodule Part1 do
  def solve(input) do
    input
    |> String.split(["\n", ",", "-"])
    |> Enum.map(&String.to_integer/1)
    |> Enum.chunk_every(4)
    |> Enum.filter(fn [fs, fe, ss, se] ->
      (fs <= ss &amp;&amp; fe >= se) || (fs >= ss &amp;&amp; fe <= se)
    end)
    |> Enum.count()
  end
end

input
|> Kino.Input.read()
|> Part1.solve()
defmodule Part2 do
  def solve(input) do
    input
    |> String.split(["\n", ",", "-"])
    |> Enum.map(&amp;String.to_integer/1)
    |> Enum.chunk_every(4)
    |> Enum.reject(fn [fs, fe, ss, se] ->
      Range.disjoint?(fs..fe, ss..se)
    end)
    |> Enum.count()
  end
end

input
|> Kino.Input.read()
|> Part2.solve()