Skip to content

Instantly share code, notes, and snippets.

@pazworld
Created July 9, 2013 05:37
Show Gist options
  • Save pazworld/5954964 to your computer and use it in GitHub Desktop.
Save pazworld/5954964 to your computer and use it in GitHub Desktop.
「サイコロを転がす」をErlangで(横へな12) ref: http://qiita.com/pazworld/items/53d3bf2ca4216cae27f0
-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]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment