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

Day 4: Camp Cleanup

2022/elixir/day-04.livemd

Day 4: Camp Cleanup

Mix.install([:kino])

Part 1

input = Kino.Input.textarea("Input")
defmodule M do
  def contains?([[s1, e1], [s2, e2]]) do
    (s1 >= s2 and e1 <= e2) or (s2 >= s1 and e2 <= e1)
  end

  def parse(pair) do
    pair
    |> String.split(",")
    |> Enum.map(fn s ->
      s
      |> String.split("-")
      |> Enum.map(&amp;String.to_integer/1)
    end)
  end
end

input
|> Kino.Input.read()
|> String.split()
|> Enum.map(&amp;M.parse/1)
|> Enum.map(&amp;M.contains?/1)
|> Enum.count(fn x -> x end)

Part 2

defmodule M do
  def overlapped?([[s1, e1], [s2, e2]]) do
    s1 <= e2 and e1 >= s2
  end

  def parse(pair) do
    pair
    |> String.split(",")
    |> Enum.map(fn s ->
      s
      |> String.split("-")
      |> Enum.map(&amp;String.to_integer/1)
    end)
  end
end

input
|> Kino.Input.read()
|> String.split()
|> Enum.map(&amp;M.parse/1)
|> Enum.map(&amp;M.overlapped?/1)
|> Enum.count(fn x -> x end)