Skip to content

Latest commit

 

History

History
47 lines (35 loc) · 728 Bytes

day04.livemd

File metadata and controls

47 lines (35 loc) · 728 Bytes

Camp Cleanup

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

Input

input = Kino.Input.textarea("Input:")
input = Kino.Input.read(input)

data =
  input
  |> String.trim()
  |> String.split()
  |> Enum.map(fn line ->
    [_ | pairs] = Regex.run(~r/(\d+)-(\d+),(\d+)-(\d+)/, line)
    [a0, a1, b0, b1] = pairs |> Enum.map(&String.to_integer/1)
    {a0..a1, b0..b1}
  end)

:ok

Part 1

contains = fn a0..a1, b0..b1 ->
  a0 >= b0 and a1 <= b1
end

part1 = data |> Enum.count(fn {a, b} -> contains.(a, b) or contains.(b, a) end)

Part 2

overlaps = fn a0..a1, b0..b1 ->
  a1 >= b0 and b1 >= a0
end

part2 = data |> Enum.count(fn {a, b} -> overlaps.(a, b) end)