2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ABC243のA, B, C問題 を Elixir で解く

Last updated at Posted at 2022-03-19

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 らしくないコードになっていると思います。

2
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?