input = Kino.Input.textarea("Put your data here")
input =
input
|> Kino.Input.read()
defmodule Day13 do
def compare(a, b) when is_integer(a) and is_list(b) do
compare([a], b)
end
def compare(a, b) when is_integer(b) and is_list(a) do
compare(a, [b])
end
def compare(a, b)
when is_integer(a) and is_integer(b) and a > b do
false
end
def compare(a, b)
when is_integer(a) and is_integer(b) and a < b do
true
end
def compare(a, b)
when is_integer(a) and is_integer(b) and a == b do
:continue
end
def compare([a_hd | a_tl], [b_hd | b_tl]) do
case compare(a_hd, b_hd) do
:continue -> compare(a_tl, b_tl)
bool -> bool
end
end
def compare([], [_ | _]) do
true
end
def compare([_ | _], []) do
false
end
def compare([], []) do
:continue
end
end
input
|> String.split("\n\n", trim: true)
|> Enum.map(fn str ->
{:ok, {_, _, list}} = Code.string_to_quoted(str)
list
end)
|> Enum.map(fn [a, b] -> {a, b} end)
|> Enum.map(fn {a, b} -> Day13.compare(a, b) end)
|> Enum.with_index()
|> Enum.filter(fn {v, _i} -> v end)
|> Enum.map(fn {_v, i} -> i + 1 end)
|> Enum.sum()
divider_packets = [[[2]], [[6]]]
input
|> String.split("\n", trim: true)
|> Enum.map(&Code.string_to_quoted(&1))
|> Enum.map(&elem(&1, 1))
|> Kernel.++(divider_packets)
|> Enum.sort(&Day13.compare(&1, &2))
|> Enum.with_index()
|> Enum.filter(fn {v, _i} -> Enum.member?(divider_packets, v) end)
|> Enum.map(fn {_v, i} -> i + 1 end)
|> Enum.product()