0
0

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 5 years have passed since last update.

「サイコロを転がす」をErlangで(横へな12)

Posted at

http://nabetani.sakura.ne.jp/hena/ord12rotdice/
「サイコロを転がす」をErlangでやってみました。

特に工夫といえる工夫もなく、サイコロを「正面、上、左」の目の組(Tuple)で表現して素直に書きました。

dice.erl
-module(dice).
-export([tests/0, test/2, solve/1]).

solve(Data) ->
  {_Dice, L} = lists:foldl(fun rotate_add/2, {{1,2,3}, [1]}, Data),
  [Digit + $0 || Digit <- lists:reverse(L)].

rotate_add(Dir, {Dice, L}) ->
  NewDice = rotate(Dir, Dice),
  {NewDice, [element(1, NewDice) | L]}.

rotate($N, {Face, Upper, Left}) -> {7 - Upper, Face, Left};
rotate($S, {Face, Upper, Left}) -> {Upper, 7 - Face, Left};
rotate($W, {Face, Upper, Left}) -> {7 - Left, Upper, Face};
rotate($E, {Face, Upper, Left}) -> {Left, Upper, 7 - Face}.

tests() ->
  test("NNESWWS", "15635624"), %0
  test("EEEE", "13641"), %1
  test("WWWW", "14631"), %2
  test("SSSS", "12651"), %3
  test("NNNN", "15621"), %4
  test("EENN", "13651"), %5
  test("WWNN", "14651"), %6
  test("SSNN", "12621"), %7
  test("NENNN", "153641"), %8
  test("NWNNN", "154631"), %9
  test("SWWWSNEEEN", "12453635421"), %10
  test("SENWSWSNSWE", "123123656545"), %11
  test("SSSWNNNE", "126546315"), %12
  test("SWNWSSSWWE", "12415423646"), %13
  test("ENNWWS", "1354135"), %14
  test("ESWNNW", "1321365"), %15
  test("NWSSE", "154135"), %16
  test("SWNWEWSEEN", "12415154135"), %17
  test("EWNWEEEEWN", "13154532426"), %18
  test("WNEWEWWWSNW", "145151562421"), %19
  test("NNEE", "15631"), %20
  test("EEEEWNWSW", "1364145642"), %21
  test("SENNWWES", "123142321"), %22
  test("SWWWSNSNESWW", "1245363635631"), %23
  test("WESSENSE", "141263231"), %24
  test("SWNSSESESSS", "124146231562"), %25
  test("ENS", "1353"), %26
  test("WNN", "1453"), %27
  test("SSEENEEEN", "1263124536"), %28
  test("NWSNNNW", "15414632"), %29
  test("ESSSSSWW", "132453215"), %30
  test("ESE", "1326"), %31
  test("SNWNWWNSSSS", "121456232453"), %32
  test("SWEESEN", "12423653"), %33
  test("NEEWNSSWWW", "15323631562"), %34
  test("WSEW", "14212"), %35
  test("SWSNNNSNWE", "12464131353"), %36
  test("ENWEWSEEW", "1351513545"), %37
  test("WSEWN", "142124"), %38
  test("EWNEESEWE", "1315321414"), %39
  test("NESEEN", "1531263"), %40
  test("WSW", "1426"), %41
  test("ENEWE", "135656"). %42

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

実行結果(の一部):


ok: NNESWWS -> 15635624
ok: EEEE -> 13641
ok: WWWW -> 14631
ok: SSSS -> 12651
ok: NNNN -> 15621
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?