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

Last updated at Posted at 2020-09-29

はじめに

問題

準備

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

問題A - Air Conditioner

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

  @doc ~S"""
  https://atcoder.jp/contests/abc174/tasks/abc174_a

  ## Examples

      iex> AtCoder174A.solve(25)
      "No"
      iex> AtCoder174A.solve(30)
      "Yes"
      iex> AtCoder174A.solve(-1)
      "No"

  """
  def solve(x) when x >= 30, do: "Yes"

  def solve(_), do: "No"
end

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

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

問題B - Distance

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

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

  @doc ~S"""
  https://atcoder.jp/contests/abc174/tasks/abc174_b

  ## Examples

      iex> AtCoder174B.solve([[0, 5], [-2, 4], [3, 4], [4, -4]], 5)
      3

  """
  def solve(list_of_lists, d) do
    square_of_d = d * d

    list_of_lists
    |> Enum.map(fn [x, y] -> x * x + y * y end)
    |> Enum.filter(&(&1 <= square_of_d))
    |> Enum.count()
  end
end
  • 提出の際にはモジュール名はMainにしておいてください
  • :tada::tada::tada:
  • 地味に入力値を読み取るところで、String.split(" ")とせずにString.split()patternを指定せずに呼び出すとタイムアウトしてしまうということがありました

問題C - Repsept

  • 問題文はリンク先をご参照くださいませ :bow:
  • 元の記事の解説をとても参考にしました!
    • ありがとうございます!
lib/at_coder_174_c.ex
defmodule AtCoder174C do
  def main do
    IO.read(:line)
    |> String.trim()
    |> String.to_integer()
    |> solve()
    |> IO.puts()
  end

  @doc ~S"""
  https://atcoder.jp/contests/abc174/tasks/abc174_c

  ## Examples

      iex> AtCoder174C.solve(101)
      4
      iex> AtCoder174C.solve(2)
      -1
      iex> AtCoder174C.solve(999983)
      999982


  """
  def solve(k) do
    1..k
    |> Enum.reduce_while({0, -1}, fn i, {a, _result} ->
      a = a * 10 + 7
      rem = rem(a, k)
      if rem == 0, do: {:halt, {rem, i}}, else: {:cont, {rem, -1}}
    end)
    |> elem(1)
  end
end
  • 提出の際にはモジュール名はMainにしておいてください
  • :tada::tada::tada:

Wrapping Up :qiita-fabicon:

  • String.split/3で入力をスペースで分割するときは第2引数のpattern" "を指定しましょう
  • Enjoy Elixir!!! :fire::rocket::rocket::rocket:
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?