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

Day 4

day4.livemd

Day 4

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

Input

input = """
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8
"""
input = Kino.Input.textarea("Paste input here:")
input = Kino.Input.read(input)

Part 1

contains? = fn [min1, max1, min2, max2] ->
  (min1 <= min2 &amp;&amp; max1 >= max2) ||
    (min1 >= min2 &amp;&amp; max1 <= max2)
end
input
|> String.split("\n", trim: true)
|> Enum.map(fn line ->
  line
  |> String.split(",")
  |> Enum.map(&amp;String.split(&amp;1, "-"))
  |> Enum.concat()
  |> Enum.map(&amp;String.to_integer/1)
end)
|> Enum.count(contains?)

Part 2

overlaps? = fn [min1, max1, min2, max2] ->
  !Range.disjoint?(min1..max1, min2..max2)
end
input
|> String.split("\n", trim: true)
|> Enum.map(fn line ->
  line
  |> String.split(",")
  |> Enum.map(&amp;String.split(&amp;1, "-"))
  |> Enum.concat()
  |> Enum.map(&amp;String.to_integer/1)
end)
|> Enum.count(overlaps?)