3
1

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

Last updated at Posted at 2020-10-05

はじめに

問題

準備

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

問題A - Sheep and Wolves

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

    solve(s, w) |> IO.puts()
  end

  @doc ~S"""
  https://atcoder.jp/contests/abc164/tasks/abc164_a

  ## Examples

      iex> Abc164A.solve(4, 5)
      "unsafe"
      iex> Abc164A.solve(100, 2)
      "safe"

  """
  def solve(s, w) when s <= w, do: "unsafe"

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

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

問題B - Battle

lib/abc_164_b.ex
defmodule Abc164B do
  def main do
    [a, b, c, d] =
      IO.read(:line) |> String.trim() |> String.split(" ") |> Enum.map(&String.to_integer/1)

    solve(a, b, c, d) |> IO.puts()
  end

  @doc ~S"""
  https://atcoder.jp/contests/abc164/tasks/abc164_b

  ## Examples

      iex> Abc164B.solve(10, 9, 10, 10)
      "No"
      iex> Abc164B.solve(46, 4, 40, 5)
      "Yes"

  """
  def solve(a, b, c, d) do
    do_solve(battle_cnt(b, c), battle_cnt(d, a))
  end

  defp do_solve(takahashi_battle_cnt, aoki_battle_cnt)
       when takahashi_battle_cnt <= aoki_battle_cnt,
       do: "Yes"

  defp do_solve(_, _), do: "No"

  defp battle_cnt(attack, hit_point) do
    Enum.reduce_while(1..100, hit_point, fn cnt, acc_hit_point ->
      acc_hit_point = acc_hit_point - attack
      if acc_hit_point <= 0, do: {:halt, cnt}, else: {:cont, acc_hit_point}
    end)
  end
end
  • 提出の際にはモジュール名はMainにしておいてください
  • :tada::tada::tada:

問題C - gacha

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

    1..n
    |> Enum.reduce([], fn _, acc ->
      [IO.read(:line) |> String.trim() | acc]
    end)
    |> solve()
    |> IO.puts()
  end

  @doc ~S"""
  https://atcoder.jp/contests/abc164/tasks/abc164_c

  ## Examples

      iex> Abc164C.solve(~w(apple orange apple))
      2

  """
  def solve(list) do
    list
    |> Enum.frequencies()
    |> map_size()
  end
end

Elixirではsizelengthに意味がそれぞれあります 💡

When you see size in a function name, it means the operation runs in constant time (also written as "O(1) time") because the size is stored alongside the data structure.
Examples: Kernel.map_size/1, Kernel.tuple_size/1

When you see length, the operation runs in linear time ("O(n) time") because the entire data structure has to be traversed.
Examples: Kernel.length/1, String.length/1

  • lengthの方はデータの数に比例して長さの取得時間が大きくなります
  • それに対して、sizeの方はデータ構造にサイズをもっているのでデータの数によらず一定時間でサイズを取得できます

Wrapping Up :qiita-fabicon:

  • 今回は自力でいけました! :smile:
  • Enjoy Elixir!!! :fire::rocket::rocket::rocket:
3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?