wslでelixir その124

# 概要
wsl(wsl2じゃない)で、elixirやってみた。
LivebookでAtCoderやってみた。

# 参考にしたページ

https://qiita.com/GeekMasahiro/items/5fce18be4a4902b9936f


# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/2f19733a-c39f-7525-e807-685b5ec1eff1.png)


# セットアップ

```
Mix.install([
    {:httpoison, "~> 2.2.1"},
    {:floki, "~> 0.35.2"},
    {:kino, "~> 0.10.0"}
])
```

# サンプルコード

abc245 a

```
contest_name = Kino.Input.text("コンテスト名")

problem_name = Kino.Input.text("問題名")

url = "https://atcoder.jp/contests/#{Kino.Input.read(contest_name)}/tasks/#{Kino.Input.read(contest_name)}_#{Kino.Input.read(problem_name)}"

doc = HTTPoison.get!(url)
{:ok, html} = Floki.parse_document(doc.body)
section_list = Floki.find(html, "section")

section_list
|> Enum.filter(fn { _, _, x} -> 
  String.starts_with?(Floki.text(x), "問題文")
end)
|> Enum.map(fn { _, _, x} ->
  Floki.text(x) 
end)
|> IO.inspect

pre_in = section_list
  |> Enum.filter(fn {_, _, [hd | _]} -> 
    String.starts_with?(Floki.text(hd), "入力例") 
  end)
  |> Enum.map(fn {_, _, [_hd | tail]} -> 
    Floki.text(hd(tail)) 
  end)
  |> Enum.map(fn x -> 
    String.replace(x, "\r\n", "\n") 
  end)
pre_out = section_list
  |> Enum.filter(fn {_, _, [hd | _]} -> 
    String.starts_with?(Floki.text(hd), "出力例") 
  end)
  |> Enum.map(fn {_, _, [_hd | tail]} -> 
    Floki.text(hd(tail)) 
  end)
  |> Enum.map(fn x -> 
    String.replace(x, "\r\n", "\n") 
  end)
{pre_in, pre_out}

defmodule Main do
  alias MyIO, as: IO
  def next_token(acc \\ "") do
    case IO.getn(:stdio, "", 1) do
      " " -> 
        acc
      "\n" -> 
        acc
      x -> 
        next_token(acc <> x)
    end
  end
  def input(), do: IO.read(:line) |> String.trim()
  def ii(), do: next_token() |> String.to_integer()
  def li(), do: input() |> String.split(" ") |> Enum.map(&String.to_integer/1)
  def f(h, m, s), do: 3600 * h + 60 * m + s
	defp solve([a, b, c, d]) do
		if f(a, b, 0) < f(c, d, 1), do: "Takahashi", else: "Aoki"
	end
  def main() do
    li()
		|> solve()
		|> IO.puts()
  end
end

defmodule MyIO do
  def read(_device \\ nil, option) do
    {file_in, _} = Application.get_env(:my_app, :file, {:stdio, :stdio})
    IO.read(file_in, option)
  end

  def puts(_device \\ nil, item) do
    {_, file_out} = Application.get_env(:my_app, :file, {:stdio, :stdio})
    IO.puts(file_out, item)
  end

  def getn(_device \\ nil, a, b) do
    {_, file_out} = Application.get_env(:my_app, :file, {:stdio, :stdio})
    IO.getn(file_out, a, b)
  end
end

defmodule Judge do
  def judge(string_in, string_out) do
    {:ok, file} = StringIO.open(string_in)
    Application.put_env(:my_app, :file, {file, file})
    Main.main()
    {:ok, {_, result}} = StringIO.close(file)
    if string_out == result do
      IO.puts("OK")
      true
    else
      IO.inspect(string_in, label: "Input")
      IO.inspect(string_out, label: "Expected")
      IO.inspect(result, label: "Received")
      false
    end
  end
end

n =
  for {{sample_in, sample_out}, n} <- Enum.zip([pre_in, pre_out]) |> Enum.with_index() do
    IO.puts("-- TEST #{n} --")
    Judge.judge(sample_in, sample_out)
  end
  |> Enum.count()

"Passed #{n} of #{length(pre_in)}"


```

# 実行結果

```
"Passed 3 of 3"

```

以上。