Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

abc245 c

stage2/at2.livemd

abc245 c

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

Section

contest_name = Kino.Input.text("コンテスト名")
problem_name = Kino.Input.text("問題名")
nyuusyu = Kino.Control.button("問題文入手")

Kino.listen(nyuusyu, fn _event ->
  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()
end)

Kino.Layout.grid([contest_name, problem_name, nyuusyu], columns: 1)
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(&amp;String.to_integer/1)

  defp output({x, y, _, _, _, _, _}) do
    if x || y, do: "Yes", else: "No"
  end

  def judge(_, {x, y, a0, b0, [a | as], [b | bs], k}) do
    x1 = judge(x, a, a0, k)
    x2 = judge(y, a, b0, k)
    y1 = judge(x, b, a0, k)
    y2 = judge(y, b, b0, k)
    {x1 || x2, y1 || y2, a, b, as, bs, k}
  end

  defp judge(f, n, n0, k), do: f &amp;&amp; abs(n - n0) <= k
  defp solve([1, _], _, _), do: "Yes"

  defp solve([n, k], [ai | as], [bi | bs]) do
    1..(n - 1)
    |> Enum.reduce({true, true, ai, bi, as, bs, k}, &amp;judge/2)
    |> output()
  end

  def main() do
    nk = li()
    as = li()
    bs = li()

    solve(nk, as, bs)
    |> 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
judge_all = Kino.Control.button("入力例で実行")

Kino.listen(judge_all, fn _event ->
  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")

  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}
  |> IO.inspect()

  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)}"
end)

Kino.Layout.grid([judge_all], columns: 1)