Rails on Tiles (어떻게 쓰기) (Elixir)
15238 단어 Elixir
소개
Elixir 즐기십니까
@ 나베타 님의 「 오프라인 실시간 어떻게 쓰는 제5회 참고문제 」를 Elixir 로 해 보았습니다
@obelisk68의 "Rails on Tiles (어떻게 쓰기)"에서 문제의 존재를 알았습니다.
준비
Elixir을 설치합시다
잘못하면
elixirjp.slack.com slack workspace 또는 NervesJP workspace에 들어와서
@torifukukaiou
에 문의하십시오.mix new
$ mix new rails_on_tiles
$ cd rails_on_tiles
소스 코드 작성
lib/rails_on_tiles.exdefmodule RailsOnTiles do
@moduledoc """
Documentation for `RailsOnTiles`.
"""
@tiles [[2, 3, 0, 1], [1, 0, 3, 2], [3, 2, 1, 0]]
@dirs [[0, -1], [1, 0], [0, 1], [-1, 0]]
@alphabets ?A..?I
@doc """
Rails on Tiles(どう書く)
## Examples
iex> RailsOnTiles.go("101221102")
"BEDGHIFEH"
iex> RailsOnTiles.go("000000000")
"BEH"
iex> RailsOnTiles.go("111111111")
"BCF"
iex> RailsOnTiles.go("222222222")
"BAD"
iex> RailsOnTiles.go("000211112")
"BEFIHEDGH"
iex> RailsOnTiles.go("221011102")
"BADGHIFEBCF"
iex> RailsOnTiles.go("201100112")
"BEHIFCBADEF"
iex> RailsOnTiles.go("000111222")
"BEFIH"
iex> RailsOnTiles.go("012012012")
"BC"
iex> RailsOnTiles.go("201120111")
"BEDABCFI"
iex> RailsOnTiles.go("220111122")
"BADEHGD"
iex> RailsOnTiles.go("221011022")
"BADG"
iex> RailsOnTiles.go("111000112")
"BCFIHEBA"
iex> RailsOnTiles.go("001211001")
"BEFI"
iex> RailsOnTiles.go("111222012")
"BCFEHIF"
iex> RailsOnTiles.go("220111211")
"BADEHI"
iex> RailsOnTiles.go("211212212")
"BCFEBAD"
iex> RailsOnTiles.go("002112210")
"BEFC"
iex> RailsOnTiles.go("001010221")
"BEF"
iex> RailsOnTiles.go("100211002")
"BEFIHG"
iex> RailsOnTiles.go("201212121")
"BEFCBAD"
"""
def go(input) do
String.codepoints(input)
|> Enum.map(&String.to_integer/1)
|> Enum.chunk_every(3)
|> solve()
|> Enum.map(&Enum.at(@alphabets, &1))
|> Enum.map(&<<&1>>)
|> Enum.join()
end
defp solve(field) do
{x, y} = new_xy(1, -1, 2)
do_solve(field, x, y, 2, [])
end
defp do_solve(_field, x, y, _dir, route) when x < 0 or x > 2 or y < 0 or y > 2, do: route
defp do_solve(field, x, y, dir, route) do
rev = get_in(@tiles, [Access.at(0), Access.at(dir)])
nxt =
get_in(@tiles, [
get_in(field, [Access.at(y), Access.at(x)]) |> Access.at(),
Access.at(rev)
])
tile_num = y * 3 + x
{x, y} = new_xy(x, y, nxt)
do_solve(field, x, y, nxt, route ++ [tile_num])
end
defp new_xy(x, y, dir) do
[dx, dy] = Enum.at(@dirs, dir)
x = x + dx
y = y + dy
{x, y}
end
end
Doctests
$ mix new rails_on_tiles
$ cd rails_on_tiles
lib/rails_on_tiles.ex
defmodule RailsOnTiles do
@moduledoc """
Documentation for `RailsOnTiles`.
"""
@tiles [[2, 3, 0, 1], [1, 0, 3, 2], [3, 2, 1, 0]]
@dirs [[0, -1], [1, 0], [0, 1], [-1, 0]]
@alphabets ?A..?I
@doc """
Rails on Tiles(どう書く)
## Examples
iex> RailsOnTiles.go("101221102")
"BEDGHIFEH"
iex> RailsOnTiles.go("000000000")
"BEH"
iex> RailsOnTiles.go("111111111")
"BCF"
iex> RailsOnTiles.go("222222222")
"BAD"
iex> RailsOnTiles.go("000211112")
"BEFIHEDGH"
iex> RailsOnTiles.go("221011102")
"BADGHIFEBCF"
iex> RailsOnTiles.go("201100112")
"BEHIFCBADEF"
iex> RailsOnTiles.go("000111222")
"BEFIH"
iex> RailsOnTiles.go("012012012")
"BC"
iex> RailsOnTiles.go("201120111")
"BEDABCFI"
iex> RailsOnTiles.go("220111122")
"BADEHGD"
iex> RailsOnTiles.go("221011022")
"BADG"
iex> RailsOnTiles.go("111000112")
"BCFIHEBA"
iex> RailsOnTiles.go("001211001")
"BEFI"
iex> RailsOnTiles.go("111222012")
"BCFEHIF"
iex> RailsOnTiles.go("220111211")
"BADEHI"
iex> RailsOnTiles.go("211212212")
"BCFEBAD"
iex> RailsOnTiles.go("002112210")
"BEFC"
iex> RailsOnTiles.go("001010221")
"BEF"
iex> RailsOnTiles.go("100211002")
"BEFIHG"
iex> RailsOnTiles.go("201212121")
"BEFCBAD"
"""
def go(input) do
String.codepoints(input)
|> Enum.map(&String.to_integer/1)
|> Enum.chunk_every(3)
|> solve()
|> Enum.map(&Enum.at(@alphabets, &1))
|> Enum.map(&<<&1>>)
|> Enum.join()
end
defp solve(field) do
{x, y} = new_xy(1, -1, 2)
do_solve(field, x, y, 2, [])
end
defp do_solve(_field, x, y, _dir, route) when x < 0 or x > 2 or y < 0 or y > 2, do: route
defp do_solve(field, x, y, dir, route) do
rev = get_in(@tiles, [Access.at(0), Access.at(dir)])
nxt =
get_in(@tiles, [
get_in(field, [Access.at(y), Access.at(x)]) |> Access.at(),
Access.at(rev)
])
tile_num = y * 3 + x
{x, y} = new_xy(x, y, nxt)
do_solve(field, x, y, nxt, route ++ [tile_num])
end
defp new_xy(x, y, dir) do
[dx, dy] = Enum.at(@dirs, dir)
x = x + dx
y = y + dy
{x, y}
end
end
Doctests
## Examples
아래에있는 것은 Doctests이라는 것으로 테스트 할 수 있습니다 $ mix test
......................
Finished in 0.08 seconds
21 doctests, 1 test, 0 failures
Wrapping Up 🎍🎍🎍🎍🎍
Reference
이 문제에 관하여(Rails on Tiles (어떻게 쓰기) (Elixir)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/torifukukaiou/items/8c684fec556a132efe3f텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)