Elixir 初心者が AtCoder に挑戦してみたのですが、なかなか Elixir でやっている人がいないので、敢て恥を晒します。クソコードをボロボロに殴ってやって下さい。
A問題: Shampoo
defmodule Main do
def main do
IO.read(:line)
|> String.trim()
|> String.split(" ")
|> Enum.map(&String.to_integer/1)
|> do_solve()
|> IO.puts()
end
defp do_solve([v, a, b, c]), do: rem(v, a + b + c) |> f(a, b)
defp f(r, a, _) when r < a, do: "F"
defp f(r, a, b) when r < a + b, do: "M"
defp f(_, _, _), do: "T"
end
B問題: Hit and Blow
defmodule Main do
def main do
IO.read(:line)
as = IO.read(:line) |> list_to_i()
bs = IO.read(:line) |> list_to_i()
do_solve(as, bs)
end
defp list_to_i(str) do
str
|> String.trim()
|> String.split(" ")
|> Enum.map(&String.to_integer/1)
end
defp do_solve(as, bs) do
as1 = MapSet.new(as)
bs1 = MapSet.new(bs)
i = MapSet.intersection(as1, bs1) |> Enum.count()
j = Enum.zip(as, bs) |> Enum.count(fn {a, b} -> a == b end)
IO.puts(j)
IO.puts(i - j)
end
end
C問題: Collision 2
defmodule Main do
def main do
n = IO.read(:line) |> String.trim() |> String.to_integer()
xys = 1..n |> Enum.map(&list_to_i(&1))
s = IO.read(:line) |> String.trim()
do_solve(xys, s) |> output() |> IO.puts()
end
defp list_to_i(_) do
IO.read(:line)
|> String.trim()
|> String.split(" ")
|> Enum.map(&String.to_integer/1)
end
defp do_solve(xys, s) do
Enum.zip(xys, String.to_charlist(s))
|> Enum.group_by(fn {[_, y], _} -> y end)
|> Enum.any?(fn {_, list} -> f(list) end)
end
defp f(list) do
try do
r = list
|> Enum.filter(fn {_, c} -> c == 76 end) #76は"L"
|> Enum.map(fn {[x, _], _} -> x end)
|> Enum.max()
l = list
|> Enum.filter(fn {_, c} -> c == 82 end) #82は"R"
|> Enum.map(fn {[x, _], _} -> x end)
|> Enum.min()
l < r
rescue
_ in Enum.EmptyError -> false
end
end
defp output(true), do: "Yes"
defp output(false), do: "No"
end
最後に
とにかく書き方がわからないので、Ruby で解いたのを下敷きにして Elixir らしくないコードになっていると思います。