Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@pazworld

「山折り谷折り」をErlangで(横へな18参考)

山折り谷折り」をErlangでやってみました。他の皆様による実装は 第18回オフラインリアルタイムどう書くの参考問題 から辿れます。

mafovafo.erl
-module(mafovafo).
-compile(export_all).

solve(Data) -> to_s(lists:foldl(fun unfold/2, [] , lists:reverse(Data))).

to_s(L) -> [case C of 0 -> $V; 1 -> $m end || C <- L].

unfold($L, L) -> rev(L) ++ [0] ++ L;
unfold($J, L) -> L ++ [0] ++ rev(L);
unfold($Z, L) -> L ++ [1] ++ rev(L) ++ [0] ++ L;
unfold($U, L) -> rev(L) ++ [0] ++ L ++ [0] ++ rev(L);
unfold($S, L) -> L ++ [0] ++ rev(L) ++ [1] ++ L.

rev(L) -> [1 - X || X <- lists:reverse(L)].

test(Data, Expected) -> test(Data, solve(Data), Expected).
test(Data, Result, Expected) -> io:fwrite("~s: ~s -> ~s~n",
  [case Result =:= Expected of true -> ok; false -> ng end, Data, Result]).

tests() ->
  test("JZ", "mVVmV"), %0
  test("J", "V"), %1
  test("L", "V"), %2
  test("Z", "mV"), %3
  test("U", "VV"), %4
  test("S", "Vm"), %5
  test("JL", "VVm"), %6
  test("JS", "VmVVm"), %7
  test("JU", "VVVmm"), %8
  test("LU", "mmVVV"), %9
  test("SL", "VVmmV"), %10
  test("SS", "VmVVmmVm"), %11
  test("SU", "VVVmmmVV"), %12
  test("SZ", "mVVmVmmV"), %13
  test("UL", "mVVVm"), %14
  test("UU", "mmVVVVmm"), %15
  test("UZ", "mVVmVVmV"), %16
  test("ZJ", "VmmVV"), %17
  test("ZS", "VmmVmVVm"), %18
  test("ZZ", "mVmmVVmV"), %19
  test("JJJ", "VVmVVmm"), %20
  test("JJZ", "mVVmVVmVmmV"), %21
  test("JSJ", "VVmmVVmVVmm"), %22
  test("JSS", "VmVVmmVmVVmVVmmVm"), %23
  test("JUS", "VmVVmVVmVVmmVmmVm"), %24
  test("JUU", "mmVVVVmmVVVmmmmVV"), %25
  test("JUZ", "mVVmVVmVVmVmmVmmV"), %26
  test("LJJ", "VmmVVVm"), %27
  test("LLS", "VmmVmVVmVVm"), %28
  test("LLU", "mmmVVVmmVVV"), %29
  test("LLZ", "mVmmVVmVVmV"), %30
  test("LSU", "mmVVVmmmVVVVmmmVV"), %31
  test("LSZ", "mVVmVmmVVmVVmVmmV"), %32
  test("LZL", "mmVVmVVmmVV"), %33
  test("LZS", "VmmVmVVmVVmmVmVVm"), %34
  test("LZU", "mmmVVVmmVVVmmmVVV"), %35
  test("SJL", "VVmVVmmmVVm"), %36
  test("SLU", "mmVVVVmmmVVmmmVVV"), %37
  test("SLZ", "mVVmVVmVmmVmmVVmV"), %38
  test("SSU", "VVVmmmVVVmmVVVmmmmVVVmmmVV"), %39
  test("SUJ", "mVVVmVVmmmVmmVVVm"), %40
  test("SUS", "VmVVmVVmVVmmVmmVmmVmVVmVVm"), %41
  test("SZZ", "mVmmVVmVVmVmmVVmVmmVmmVVmV"), %42
  test("UJJ", "VmmVVVmVVmm"), %43
  test("ULU", "mmmVVVmmVVVVmmmVV"), %44
  test("ULZ", "mVmmVVmVVmVVmVmmV"), %45
  test("UUU", "VVmmmmVVVmmVVVVmmVVVmmmmVV"), %46
  test("ZJU", "VVVmmmVVmmmVVVVmm"), %47
  test("ZLS", "VmVVmmVmmVmVVmVVm"), %48
  test("ZSJ", "VVmmVmmVVmmVVVmmV"), %49
  test("ZUJ", "mVVVmmVmmmVVmVVVm"), %50
  test("JJLJ", "mVVVmmVVmVVmmmV"), %51
  test("JLJJ", "VmmVVVmVVmmmVVm"), %52
  test("JLJL", "VmmVVVmVVmmmVVm"), %53
  test("LJJL", "VVmmVmmVVVmVVmm"), %54
  test("LLJJ", "VmmmVVmVVmmVVVm"), %55
  test("SZUS", "VmVVmVVmmVmmVmmVmVVmVVmVVmVVmmVmmVmmVmVVmVVmVVmmVmmVmmVmVVmVVmmVmmVmmVmVVmVVmVVm"), %56
  test("ULLS", "VmmVmmVmVVmVVmmVmVVmVVmVVmmVmmVmVVm"), %57
  test("JJJJZJ", "VmmVVVmmVVmVVmmVVmmmVVmVVmmVVVmmVVmmVmmVVmmmVVmVVmmVVVmmVVmVVmmVVmmmVVmmVmmVVVmmVVmmVmmVVmmmVVm"), %58
  test("JULLLJ", "mmVmmVVmmmVVmVVVmmVmmVVVmmVVmVVVmmVmmVVmmmVVmVVVmmVmmVVVmmVVmVVmmmVmmVVmmmVVmVVmmmVmmVVVmmVVmVV"), %59
  test("LJJJUL", "mVVVmVVmmmVVmVVVmmVmmmVmmVVVmVVmmmVmmVVVmmVmmmVVmVVVmVVmmmVVmVVVmmVmmmVVmVVVmVVmmmVmmVVVmmVmmmV"), %60
  test("LJSJJL", "VVmVVmmVVVmmVmmmVVmVVmmmVVmmVmmVVVmVVmmmVVmmVmmVVVmVVmmVVVmmVmmmVVmVVmmVVVmmVmmVVVmVVmmmVVmmVmm"), %61
  test("LZLLLJ", "mmVmmVVmmmVVmVVmmmVmmVVVmmVVmVVVmmVmmVVmmmVVmVVVmmVmmVVVmmVVmVVmmmVmmVVmmmVVmVVVmmVmmVVVmmVVmVV"), %62
  test("SJJJJL", "VVmVVmmVVVmmVmmVVVmVVmmmVVmmVmmVVVmVVmmVVVmmVmmmVVmVVmmmVVmmVmmmVVmVVmmVVVmmVmmVVVmVVmmmVVmmVmm"), %63
  test("ZLJLJL", "VmmVVVmmVmmmVVmVVmmVVVmVVmmmVVmmVmmVVVmmVmmmVVmmVmmVVVmVVmmmVVmVVmmVVVmmVmmmVVmVVmmVVVmVVmmmVVm"). %64

紙を折っていく問題ですが、発想を転換し、折られていない紙を薄く裂きながら開いていくことを考えました。

Erlangは関数シグニチャでパターンマッチングが使えるので、条件分岐(ifやcase)が減ってすっきりと書けました。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
0
Help us understand the problem. What are the problem?