Skip to content

Instantly share code, notes, and snippets.

@iporsut
Created May 16, 2015 11:54
Show Gist options
  • Save iporsut/3491c988d5f604ec683f to your computer and use it in GitHub Desktop.
Save iporsut/3491c988d5f604ec683f to your computer and use it in GitHub Desktop.
Alien Language Haskell
import Control.Monad (replicateM)
parseTokens:: String -> [String]
parseTokens tokenStr = parseTokens' [] "" False tokenStr
where
parseTokens' acc word isGroup "" = acc
parseTokens' acc word isGroup (x:xs)
| not isGroup && x == '(' = parseTokens' acc "" True xs
| not isGroup && x /= '(' = parseTokens' (acc++[[x]]) "" False xs
| isGroup && x /= ')' = parseTokens' acc (word++[x]) True xs
| isGroup && x == ')' = parseTokens' (acc++[word]) "" False xs
isMatch tokens word = all id $ zipWith elem word tokens
countMatch words tokenStr = sum $ map (\w -> if isMatch (parseTokens tokenStr) w then 1 else 0) words
main :: IO ()
main = do
line <- getLine
let [l,d,n] = (map (\x -> read x::Int) (words line))
dicts <- replicateM d getLine
tokensStr <- replicateM n getLine
let counts = map (\t -> countMatch dicts t) tokensStr
let countsOutput = map (\(i, count) -> "Case #" ++ (show i) ++ ": " ++ (show count)) (zip [1..n] counts)
mapM_ putStrLn countsOutput
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment