Day 05
Mix.install([:kino])
Section
content =
Kino.FS.file_path("input_05.txt")
|> File.read!()
[rules, manuals] = content |> String.split("\n\n")
rules =
rules
|> String.split(["\n", "|"])
|> Enum.map(&String.to_integer/1)
|> Enum.chunk_every(2)
|> Enum.group_by(&List.first/1, &List.last/1)
manuals =
manuals
|> String.split()
|> Enum.map(fn row -> row |> String.split(",") |> Enum.map(&String.to_integer/1) end)
defmodule Manual do
def sort(rules, manual) do
Enum.sort(manual, fn lhs, rhs ->
rhs in Map.get(rules, lhs, [])
end)
end
end
Part 1
manuals
|> Enum.filter(& &1 == Manual.sort(rules, &1))
|> Enum.map(fn list -> Enum.at(list, list |> length |> div(2)) end)
|> Enum.sum()
Part 2
manuals
|> Enum.reject(& &1 == Manual.sort(rules, &1))
|> Enum.map(&Manual.sort(rules, &1))
|> Enum.map(fn list -> Enum.at(list, list |> length |> div(2)) end)
|> Enum.sum()