1
1

More than 5 years have passed since last update.

Posted at

ハッシュのかわりにパタンマッチングを使っています（なのでちょっと行数増えました）。

``````
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")
, ("BEFIHEDGH",   "000211112")
, ("BEFIH",       "000111222")
, ("BC",          "012012012")
, ("BEDABCFI",    "201120111")
, ("BCFIHEBA",    "111000112")
, ("BEFI",        "001211001")
, ("BCFEHIF",     "111222012")
, ("BEFC",        "002112210")
, ("BEF",         "001010221")
, ("BEFIHG",      "100211002")
]
``````
1
1
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
1
1