2
3

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で(横へな14)

Last updated at Posted at 2013-10-07

眠れるモンスターを狩る」をErlangでやってみました。他の実装は 第14回オフラインリアルタイムどう書くの問題 から辿れます。

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

%% 武器・モンスター対応表 {武器, 倒せるモンスター, 得られる武器}
table() -> [{$a, $B, $c}, {$c, $D, $e}, {$e, $F, $g},
            {$g, $H, $i}, {$i, $J, $k}, {$k, $L, $a}].

%% 解く
solve(Data) ->
  Arms = lists:usort([X || X <- Data, X > $Z]), % 小文字のみ集める
  integer_to_list(hit(Arms, Data)).

%% 武器と全体のリストから倒したモンスターの数を返す
hit([], _) -> 0;
hit([Arm | RestArms], Data) ->
  {Monster, NewArm} = lookup(Arm),
  HitMonster = fun(M) -> M =:= Monster end,
  {Hits, NoHits} = lists:partition(HitMonster, Data),
  case Hits of
    [] -> hit(RestArms, Data); % その武器で倒せるモンスターが居ない
    _  -> length(Hits) + hit([NewArm | RestArms], NoHits) % 倒せた
  end.

%% 武器から倒せるモンスターと得られる武器を返す
lookup(Arm) ->
  hd([{M, NA} || {A, M, NA} <- table(), A =:= Arm]).

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

tests() ->
  test("gLDLBgBgHDaD", "6"), %0
  test("DBcDLaLgDBH", "6"), %1
  test("JJca", "0"), %2
  test("FJDLBH", "0"), %3
  test("HJBLFDg", "6"), %4
  test("HBaDLFJ", "6"), %5
  test("DJaHLB", "2"), %6
  test("gDLHJF", "3"), %7
  test("cJFgLHD", "5"), %8
  test("FFBJaJJ", "1"), %9
  test("FJeJFBJ", "2"), %10
  test("iJFFJJB", "3"), %11
  test("JBJiLFJF", "5"), %12
  test("JDiFLFBJJ", "8"), %13
  test("BDFDFFDFFLLFFJFDBFDFFFFDDFaDBFFB", "28"), %14
  test("DDFBFcBDFFFFFFLBFDFFBFLFDFDJDFDF", "24"), %15
  test("FDLBFDDBFFFeFFFFFDFBLDDFDDFBFFJF", "16"), %16
  test("FDBFFLFDFFDBBDFFBJDLFgDFFFDFFDFF", "0"), %17
  test("FDiFLDFFFFBDDJDDBFBFDFFFBFFDFLFF", "31"), %18
  test("FDFDJBLBLBFFDDFFFDFFFFFDDFBkFDFF", "30"), %19
  test("HBkFFFFHBLH", "3"), %20
  test("FBHHFFFHLaB", "2"), %21
  test("LFHFBBcHFHF", "0"), %22
  test("LFBHFFeFHBH", "7"), %23
  test("LgFHHHBFBFF", "3"), %24
  test("FFiFHBHLBFH", "0"), %25
  test("BFHHFFHBeFLk", "10"), %26
  test("FHFaBBHFHLFg", "5"), %27
  test("FFgacaFg", "0"), %28
  test("JHDaDcBJiiHccBHDBDH", "9"), %29
  test("FHJJLckFckFJHDFF", "12"), %30
  test("DeDHJHDFHJBLHDLLDHJLBDD", "22"), %31
  test("gJLLLJgJgJLJL", "0"), %32
  test("DaaaDDD", "0"), %33
  test("HFeJFHiBiiBJeJBBFFB", "9"), %34
  test("FJFFJDBHBHaLJBHJHDLHkLLLFFFgJgHJLHkJkB", "32"), %35
  test("giFLBiBJLLJgHBFJigJJJBLHFLDLL", "23"), %36
  test("cgkLJcLJJJJgJc", "2"), %37
  test("LDFHJHcFBDBLJBLFLcFJcDFBL", "22"), %38
  test("JJHHHkHJkHLJk", "1"), %39
  test("kHHBBaBgHagHgaHBBB", "11"), %40
  test("HDBFFDHHHDFLDcHHLFDcJD", "20"), %41
  test("HFFFHeFFee", "7"), %42
  test("gLLDHgDLgFL", "1"), %43
  test("JJJBBaBBHBBHaLBHJ", "7"), %44
  test("FBFBgJBDBDgF", "0"), %45
  test("LLLLakakLakLL", "7"), %46
  test("HeJHeJe", "0"), %47
  test("LDFLBLLeBLDBBFFBLFBB", "4"). %48

結果の一部:


ok: gLDLBgBgHDaD -> 6
ok: DBcDLaLgDBH -> 6
...
ok: HeJHeJe -> 0
ok: LDFLBLLeBLDBBFFBLFBB -> 4
2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?