Day 04
Mix.install([
{:kino, "~> 0.7.0"}
])
example_input =
Kino.Input.textarea("example input:")
|> Kino.render()
real_input = Kino.Input.textarea("real input:")
Common
parse = fn input ->
input
|> Kino.Input.read()
|> String.split(["-", ",", "\n"])
|> Enum.map(&String.to_integer/1)
|> Enum.chunk_every(4)
|> Enum.map(fn [a, b, c, d] -> {Range.new(a, b), Range.new(c, d)} end)
end
Part 1
contained? = fn {a, b} ->
(a.first in b and a.last in b) or (b.first in a and b.last in a)
end
real_input
|> then(parse)
|> Enum.filter(contained?)
|> length()
Part 2
overlaps? = fn {a, b} -> not Range.disjoint?(a, b) end
real_input
|> then(parse)
|> Enum.filter(overlaps?)
|> length()