Day 3: Rucksack Reorganization
Mix.install([:kino])
Part 1
input = Kino.Input.textarea("Input")
defmodule M do
def score(str) do
str
|> String.split_at(div(String.length(str), 2))
|> then(fn {l, r} ->
MapSet.intersection(to_set(l), to_set(r))
end)
|> MapSet.to_list()
|> hd()
|> then(fn
n when n >= ?a -> n - ?a + 1
n when n >= ?A -> n - ?A + 1 + 26
end)
end
defp to_set(str) do
str
|> String.to_charlist()
|> MapSet.new()
end
end
input
|> Kino.Input.read()
|> String.split()
|> Enum.map(&M.score/1)
|> Enum.sum()
Part 2
defmodule M do
def score(group) do
group
|> Enum.map(&to_set/1)
|> Enum.reduce(&MapSet.intersection/2)
|> MapSet.to_list()
|> hd()
|> then(fn
n when n >= ?a -> n - ?a + 1
n when n >= ?A -> n - ?A + 1 + 26
end)
end
defp to_set(str) do
str
|> String.to_charlist()
|> MapSet.new()
end
end
input
|> Kino.Input.read()
|> String.split()
|> Enum.chunk_every(3)
|> Enum.map(&M.score/1)
|> Enum.sum()