Help us understand the problem. What is going on with this article?

オフラインリアルタイムどう書く第5回の参考問題の回答例(Haskell)

More than 5 years have passed since last update.

問題はこちら。 http://qiita.com/items/0ddde0164a745cd09c34

Ruby版の回答 ( http://qiita.com/items/ed5fa743164638de72b1 ) をHaskellに翻訳してみました。

左右の番兵をひとつですませる発想はなかったので、ちょっと新鮮でした。
ハッシュのかわりにパタンマッチングを使っています(なのでちょっと行数増えました)。

import Data.Char
import Test.HUnit

board = "----ABC-DEF-GHI----"

move 0   4  =  4
move 0 (-4) = -4
move 0   1  =  1
move 0 (-1) = -1
move 1   4  =  1
move 1 (-4) = -1
move 1   1  =  4
move 1 (-1) = -4
move 2   4  = -1
move 2 (-4) =  1
move 2   1  = -4
move 2 (-1) =  4

solve xs = solve' 5 4
    where
        solve' pos mv
            | pat !! pos < 0 = []
            | otherwise      = let mv'  = move (pat !! pos) mv
                                   pos' = pos + mv'
                               in  (board !! pos):(solve' pos' mv')
        pat = replicate 4 (-1) ++ (pat' $ map ((subtract 48).ord) xs)
        pat' []        = replicate 4 (-1)
        pat' (a:b:c:t) = a:b:c:(-1):(pat' t)

main = do
    runTestTT $ test $ map (\(e, a) -> e ~=? solve a) testPattern
    return ()

testPattern = [ ("BEDGHIFEH",   "101221102")
              , ("BEH",         "000000000")
              , ("BCF",         "111111111")
              , ("BAD",         "222222222")
              , ("BEFIHEDGH",   "000211112")
              , ("BADGHIFEBCF", "221011102")
              , ("BEHIFCBADEF", "201100112")
              , ("BEFIH",       "000111222")
              , ("BC",          "012012012")
              , ("BEDABCFI",    "201120111")
              , ("BADEHGD",     "220111122")
              , ("BADG",        "221011022")
              , ("BCFIHEBA",    "111000112")
              , ("BEFI",        "001211001")
              , ("BCFEHIF",     "111222012")
              , ("BADEHI",      "220111211")
              , ("BCFEBAD",     "211212212")
              , ("BEFC",        "002112210")
              , ("BEF",         "001010221")
              , ("BEFIHG",      "100211002")
              , ("BEFCBAD",     "201212121")
              ]
emattsan
ソフトウェアエンジニア、言語オタク
http://d.hatena.ne.jp/E_Mattsan/
esm
We build applications which work well and make customers happy.
http://www.esm.co.jp
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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした