第18回オフラインリアルタイムどう書くの参考問題を Haskell で解く


第18回オフラインリアルタイムどう書くの参考問題 を解きました。

foldBase val (x:xs)
  | x == 'F' = val ++ foldBase val xs
  | x == 'R' = reverse(invert val) ++ foldBase val xs
  | otherwise = x : foldBase val xs
foldBase _ [] = ""

invert ('F':xs) = 'R' : invert xs
invert ('R':xs) = 'F' : invert xs
invert ('m':xs) = 'V' : invert xs
invert ('V':xs) = 'm' : invert xs
invert [] = ""

foldL = foldBase "RVF"
foldJ = foldBase "FVR"
foldZ = foldBase "FmRVF"
foldU = foldBase "RVFVR"
foldS = foldBase "FVRmF"

filter' = filter(\x -> x == 'm' || x == 'V')

foldX x
  | x == 'L' = foldL
  | x == 'J' = foldJ
  | x == 'Z' = foldZ
  | x == 'U' = foldU
  | x == 'S' = foldS

solve xs = filter' $ foldl (\acc x -> foldX x acc) "F" xs

test input expected
  | solve input == expected = putStrLn "OK"
  | otherwise = putStrLn $ "NG : " ++ input

main = do
  test "JZ" "mVVmV"
  test "J" "V"
  test "L" "V"
  test "Z" "mV"
  test "U" "VV"
  test "S" "Vm"
  test "JL" "VVm"
  test "JS" "VmVVm"
  test "JU" "VVVmm"
  test "LU" "mmVVV"
  test "SL" "VVmmV"
  test "SS" "VmVVmmVm"
  test "SU" "VVVmmmVV"
  test "SZ" "mVVmVmmV"
  test "UL" "mVVVm"
  test "UU" "mmVVVVmm"
  test "UZ" "mVVmVVmV"
  test "ZJ" "VmmVV"
  test "ZS" "VmmVmVVm"
  test "ZZ" "mVmmVVmV"
  test "JJJ" "VVmVVmm"
  test "JJZ" "mVVmVVmVmmV"
  test "JSJ" "VVmmVVmVVmm"
  test "JSS" "VmVVmmVmVVmVVmmVm"
  test "JUS" "VmVVmVVmVVmmVmmVm"
  test "JUU" "mmVVVVmmVVVmmmmVV"
  test "JUZ" "mVVmVVmVVmVmmVmmV"
  test "LJJ" "VmmVVVm"
  test "LLS" "VmmVmVVmVVm"
  test "LLU" "mmmVVVmmVVV"
  test "LLZ" "mVmmVVmVVmV"
  test "LSU" "mmVVVmmmVVVVmmmVV"
  test "LSZ" "mVVmVmmVVmVVmVmmV"
  test "LZL" "mmVVmVVmmVV"
  test "LZS" "VmmVmVVmVVmmVmVVm"
  test "LZU" "mmmVVVmmVVVmmmVVV"
  test "SJL" "VVmVVmmmVVm"
  test "SLU" "mmVVVVmmmVVmmmVVV"
  test "SLZ" "mVVmVVmVmmVmmVVmV"
  test "SSU" "VVVmmmVVVmmVVVmmmmVVVmmmVV"
  test "SUJ" "mVVVmVVmmmVmmVVVm"
  test "SUS" "VmVVmVVmVVmmVmmVmmVmVVmVVm"
  test "SZZ" "mVmmVVmVVmVmmVVmVmmVmmVVmV"
  test "UJJ" "VmmVVVmVVmm"
  test "ULU" "mmmVVVmmVVVVmmmVV"
  test "ULZ" "mVmmVVmVVmVVmVmmV"
  test "UUU" "VVmmmmVVVmmVVVVmmVVVmmmmVV"
  test "ZJU" "VVVmmmVVmmmVVVVmm"
  test "ZLS" "VmVVmmVmmVmVVmVVm"
  test "ZSJ" "VVmmVmmVVmmVVVmmV"
  test "ZUJ" "mVVVmmVmmmVVmVVVm"
  test "JJLJ" "mVVVmmVVmVVmmmV"
  test "JLJJ" "VmmVVVmVVmmmVVm"
  test "JLJL" "VmmVVVmVVmmmVVm"
  test "LJJL" "VVmmVmmVVVmVVmm"
  test "LLJJ" "VmmmVVmVVmmVVVm"
  test "SZUS" "VmVVmVVmmVmmVmmVmVVmVVmVVmVVmmVmmVmmVmVVmVVmVVmmVmmVmmVmVVmVVmmVmmVmmVmVVmVVmVVm"
  test "ULLS" "VmmVmmVmVVmVVmmVmVVmVVmVVmmVmmVmVVm"
  test "JJJJZJ" "VmmVVVmmVVmVVmmVVmmmVVmVVmmVVVmmVVmmVmmVVmmmVVmVVmmVVVmmVVmVVmmVVmmmVVmmVmmVVVmmVVmmVmmVVmmmVVm"
  test "JULLLJ" "mmVmmVVmmmVVmVVVmmVmmVVVmmVVmVVVmmVmmVVmmmVVmVVVm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