Skip to content

Instantly share code, notes, and snippets.

@JDevlieghere
Created February 13, 2015 17:07
Show Gist options
  • Save JDevlieghere/2e6c30e2b57ada6c6bae to your computer and use it in GitHub Desktop.
Save JDevlieghere/2e6c30e2b57ada6c6bae to your computer and use it in GitHub Desktop.
toSet :: ByteString -> Set ByteString
toSet = Set.fromList . B.words . B.map toLower
toWords :: ByteString -> [ByteString]
toWords s
| B.null s = []
| otherwise = B.inits s ++ toWords (B.tail s)
solve :: Set ByteString -> [ByteString] -> [ByteString]
solve dict strs = filter valid wrds
where wrds = foldr ((++) . toWords) [] strs
valid w = B.length w > 3 && Set.member w dict
main :: IO ()
main = do
(dfile:ifile:_) <- getArgs
dictionary <- B.readFile dfile
input <- B.readFile ifile
let dict = toSet dictionary
rows = (B.lines . B.map toLower) input
cols = B.transpose rows
solutions = solve dict (rows ++ cols) `using` parList rseq
putStrLn $ B.unpack $ B.unlines solutions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment