ABC088B - Card Game for Two
問題
回答1
defmodule Main do
def main do
:stdio
|> IO.read(:all)
|> solve()
|> IO.puts()
end
defp split_lines(lines) do
lines
|> String.trim()
|> String.split("\n")
end
defp split_words(words) do
String.split(words, " ")
end
def solve(input) do
input
|> split_lines()
|> Enum.at(1)
|> split_words()
|> Enum.map(&String.to_integer/1)
|> Enum.sort(:desc)
|> Enum.chunk_every(2)
|> Enum.map(fn
[a, b] -> a - b
[a] -> a
end)
|> Enum.sum()
end
end
"""
2
3 1
"""
|> Main.solve()
|> then(&(&1 == 2))
"""
3
2 7 4
"""
|> Main.solve()
|> then(&(&1 == 5))
"""
4
20 18 2 18
"""
|> Main.solve()
|> then(&(&1 == 18))