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 3 years have passed since last update.

ElixirでABC166のA,B,C問題を制する!

Last updated at Posted at 2020-10-03

はじめに

  • Elixirでやってみました

問題

準備

  • Elixirをインストールしましょう
  • プロジェクトを作っておきます
$ mix new at_coder
$ cd at_coder

問題A - A?C

  • 問題文はリンク先をご参照くださいませ :bow:
lib/abc_166_a.ex
defmodule Abc166A do
  def main do
    IO.read(:line)
    |> String.trim()
    |> solve()
    |> IO.puts()
  end

  @doc ~S"""
  https://atcoder.jp/contests/abc166/tasks/abc166_a

  ## Examples

      iex> Abc166A.solve("ABC")
      "ARC"
      iex> Abc166A.solve("ARC")
      "ABC"

  """
  def solve(s) when s == "ABC", do: "ARC"

  def solve(_s), do: "ABC"
end

  • ## Examplesのところに書いてあるものは、Doctestsと呼ばれるものでしてテストができます
  • 解答のキモとなる関数について、問題に書いてある入力例をインプットして出力例の通りアウトプットされるかを確かめています
  • test/at_coder_test.exsに設定を足しておきましょう
test/at_coder_test.exs
defmodule AtCoderTest do
  use ExUnit.Case
  doctest Abc166A
$ mix test
..........

Finished in 0.2 seconds
9 doctests, 1 test, 0 failures
  • 提出の際にはモジュール名はMainにしておいてください
  • :tada::tada::tada:
  • 以下、B問題、C問題を解いていきます

問題B - Trick or Treat

lib/abc_166_b.ex
defmodule Abc166B do
  def main do
    [n, k] =
      IO.read(:line)
      |> String.trim()
      |> String.split(" ")
      |> Enum.map(&String.to_integer/1)

    1..k
    |> Enum.reduce([], fn _, acc ->
      IO.read(:line)

      list =
        IO.read(:line) |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1)

      [list | acc]
    end)
    |> List.flatten()
    |> solve(n)
    |> IO.puts()
  end

  @doc ~S"""
  https://atcoder.jp/contests/abc166/tasks/abc166_a

  ## Examples

      iex> Abc166B.solve([1, 3, 3], 3)
      1
      iex> Abc166B.solve([3, 3, 3], 3)
      2

  """
  def solve(list, n) do
    MapSet.new(list)
    |> MapSet.size()
    |> Kernel.-(n)
    |> Kernel.*(-1)
  end
end


  • 提出の際にはモジュール名はMainにしておいてください
  • :tada::tada::tada:

問題C - Peaks

  • 問題文はリンク先をご参照くださいませ :bow:
lib/abc_166_c.ex
defmodule Abc166C do
  def main do
    [n, m] =
      IO.read(:line)
      |> String.trim()
      |> String.split(" ")
      |> Enum.map(&String.to_integer/1)

    h_list =
      IO.read(:line) |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1)

    1..m
    |> Enum.reduce([], fn _, acc ->
      a_b = IO.read(:line) |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1)
      [a_b | acc]
    end)
    |> solve(h_list, n)
    |> IO.puts()
  end

  @doc ~S"""
  https://atcoder.jp/contests/abc166/tasks/abc166_c

  ## Examples

      iex> Abc166C.solve([[1, 3], [2, 3],[2, 4]], [1, 2, 3, 4], 4)
      2
      iex> Abc166C.solve([[1, 3], [4, 2],[4, 3], [4, 6], [4, 6]], [8, 6, 9, 1, 2, 1], 6)
      3

  """
  def solve(a_b_list, h_list, n) do
    h_map = Enum.zip(1..n, h_list) |> Map.new()
    peaks_map = for(i <- 1..n, do: {i, true}) |> Map.new()

    a_b_list
    |> Enum.reduce(peaks_map, fn [a, b], acc_map ->
      a_value = Map.get(acc_map, a)
      b_value = Map.get(acc_map, b)
      a_hight = Map.get(h_map, a)
      b_hight = Map.get(h_map, b)

      do_solve(acc_map, a, b, a_value, b_value, a_hight, b_hight)
    end)
    |> Enum.count(fn {_, b} -> b end)
  end

  defp do_solve(map, _a, _b, a_value, b_value, _a_hight, _b_hight)
       when not a_value and not b_value,
       do: map

  defp do_solve(map, _a, b, a_value, b_value, a_hight, b_hight)
       when a_value and b_value and a_hight > b_hight do
    Map.update!(map, b, &(!&1))
  end

  defp do_solve(map, a, b, a_value, b_value, a_hight, b_hight)
       when a_value and b_value and a_hight == b_hight do
    Map.update!(map, a, &(!&1)) |> Map.update!(b, &(!&1))
  end

  defp do_solve(map, a, _b, a_value, b_value, a_hight, b_hight)
       when a_value and b_value and a_hight < b_hight do
    Map.update!(map, a, &(!&1))
  end

  defp do_solve(map, _a, _b, a_value, b_value, a_hight, b_hight)
       when a_value and not b_value and a_hight > b_hight do
    map
  end

  defp do_solve(map, a, _b, a_value, b_value, a_hight, b_hight)
       when a_value and not b_value and a_hight == b_hight do
    Map.update!(map, a, &(!&1))
  end

  defp do_solve(map, a, _b, a_value, b_value, a_hight, b_hight)
       when a_value and not b_value and a_hight < b_hight do
    Map.update!(map, a, &(!&1))
  end

  defp do_solve(map, _a, b, a_value, b_value, a_hight, b_hight)
       when not a_value and b_value and a_hight > b_hight do
    Map.update!(map, b, &(!&1))
  end

  defp do_solve(map, _a, b, a_value, b_value, a_hight, b_hight)
       when not a_value and b_value and a_hight == b_hight do
    Map.update!(map, b, &(!&1))
  end

  defp do_solve(map, _a, _b, a_value, b_value, a_hight, b_hight)
       when not a_value and b_value and a_hight < b_hight do
    map
  end
end
  • 提出の際にはモジュール名はMainにしておいてください
  • :tada::tada::tada:

Wrapping Up :qiita-fabicon:

2
0
0

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?