1
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でABC170のA,B,C問題を制する!

Last updated at Posted at 2020-10-02

はじめに

  • Elixirでやってみました

問題

準備

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

問題A - Five Variables

  • 問題文はリンク先をご参照くださいませ :bow:
lib/abc_170_a.ex
defmodule Abc170A do
  def main do
    IO.read(:line)
    |> String.trim()
    |> String.split(" ")
    |> Enum.map(&String.to_integer/1)
    |> solve()
    |> IO.puts()
  end

  @doc ~S"""
  https://atcoder.jp/contests/abc170/tasks/abc170_b

  ## Examples

      iex> Abc170A.solve([0, 2, 3, 4, 5])
      1
      iex> Abc170A.solve([1, 2, 0, 4, 5])
      3

  """
  def solve(list) do
    Enum.find_index(list, &(&1 == 0)) + 1
  end
end

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

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

問題B - Crane and Turtle

lib/abc_170_b.ex
defmodule Abc170B do
  def main do
    [x, y] =
      IO.read(:line) |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1)

    solve(x, y)
    |> IO.puts()
  end

  @doc ~S"""
  https://atcoder.jp/contests/abc170/tasks/abc170_b

  ## Examples

      iex> Abc170B.solve(3, 8)
      "Yes"
      iex> Abc170B.solve(2, 100)
      "No"
      iex> Abc170B.solve(1, 2)
      "Yes"

  """
  def solve(x, y) when y - 2 * x >= 0 and x - div(y - 2 * x, 2) >= 0 and rem(y - 2 * x, 2) == 0,
    do: "Yes"

  def solve(_x, _y), do: "No"
end

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

問題C - Forbidden List

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

    if n > 0 do
      IO.read(:line)
      |> String.trim()
      |> String.split(" ")
      |> Enum.map(&String.to_integer/1)
      |> solve(x)
      |> IO.puts()
    else
      IO.read(:line)

      solve([], x)
      |> IO.puts()
    end
  end

  @doc ~S"""
  https://atcoder.jp/contests/abc170/tasks/abc170_c

  ## Examples

      iex> Abc170C.solve([4, 7, 10, 6, 5], 6)
      8
      iex> Abc170C.solve([4, 7, 10, 6, 5], 10)
      9
      iex> Abc170C.solve([], 100)
      100

  """
  def solve([], x), do: x

  def solve(list, x) do
    0..100
    |> Enum.reduce_while(x, fn i, acc ->
      minus_one = x - i

      if !(minus_one in list) do
        {:halt, minus_one}
      else
        plus_one = x + i

        if !(plus_one in list) do
          {:halt, plus_one}
        else
          {:cont, acc}
        end
      end
    end)
  end
end


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

Wrapping Up :qiita-fabicon:

1
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
1
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?