Day 02
Mix.install([
{:kino, "~> 0.7.0"}
])
example_input =
Kino.Input.textarea("example input:")
|> Kino.render()
real_input = Kino.Input.textarea("real input:")
Common
scores = %{
?A => 1,
?B => 2,
?C => 3,
?X => 1,
?Y => 2,
?Z => 3
}
parse = fn input ->
input
|> Kino.Input.read()
|> String.split("\n")
|> Enum.map(fn str ->
str
|> String.to_charlist()
|> then(fn [other, _, self] -> {other, self} end)
end)
end
score = fn list ->
list
|> Enum.reduce(0, fn
{m, m}, sum -> sum + m + 3
{1, 3}, sum -> sum + 3
{3, 1}, sum -> sum + 1 + 6
{other, self}, sum when other > self -> sum + self
{_other, self}, sum -> sum + self + 6
end)
end
Part 1
convert = fn list ->
list
|> Enum.map(fn {other, self} -> {scores[other], scores[self]} end)
end
real_input
|> parse.()
|> convert.()
|> score.()
Part 2
convert = fn list ->
list
|> Enum.map(fn {other, self} -> {scores[other], self} end)
|> Enum.map(fn
{other, ?Y} -> {other, other}
{1, ?X} -> {1, 3}
{other, ?X} -> {other, other - 1}
{3, ?Z} -> {3, 1}
{other, ?Z} -> {other, other + 1}
end)
end
real_input
|> parse.()
|> convert.()
|> score.()