Mix.install([
{:kino, "~> 0.7.0"}
])
input = Kino.Input.textarea("Input file:")
input
|> Kino.Input.read()
|> String.split("\n", trim: true)
|> Enum.map(&String.to_integer/1)
|> Enum.chunk_every(2, 1, :discard)
|> Enum.count(fn [left, right] -> left < right end)
input
|> Kino.Input.read()
|> String.split("\n", trim: true)
|> Enum.map(&String.to_integer/1)
|> Enum.chunk_every(3, 1, :discard)
|> Enum.chunk_every(2, 1, :discard)
|> Enum.count(fn [left, right] -> Enum.sum(left) < Enum.sum(right) end)
For a small improvement, we can note that we actually don't need to do any sums. When comparing two adjacent sums, say
input
|> Kino.Input.read()
|> String.split("\n", trim: true)
|> Enum.map(&String.to_integer/1)
|> Enum.chunk_every(4, 1, :discard)
|> Enum.count(fn [left, _, _, right] -> left < right end)