@@ -17,28 +17,35 @@ solveCSP str = [d | d <- domains, and $ map (\x -> evalCmp x d) constraints]
1717 domains = valuations (map parseDomain (split ' ,' domain))
1818 constraints = map toComparison (split ' ,' constr)
1919
20+ -- Given a constraint satisfaction problem, returns a 2-tuple containing the
21+ -- intervals in the first member and the constraints in the second member
2022splitCSP :: String -> (String , String )
2123splitCSP str = (domain, constr)
2224 where domain = takeWhile (/= ' }' ) (drop ldomain str)
2325 constr = (tail . init ) (drop (length domain + ldomain + lconstr) str)
2426 ldomain = length " domains{"
2527 lconstr = length " constraints{"
2628
29+ -- Splits a string delimited by a character into its substrings
2730split :: Char -> String -> [String ]
2831split _ [] = []
2932split c (str) = takeWhile (/= c) str : split c rem
3033 where end = (dropWhile (/= c) str)
3134 rem = if null end then [] else tail end
3235
36+ -- Parses a single interval and returns a 2-tuple containing the name of the
37+ -- interval and the size of the interval. Because there are no regexes in
38+ -- Haskell this function is a bit FUBAR.
3339parseDomain :: String -> (Name , Domain )
3440parseDomain str = (name, domain)
3541 where name = takeWhile (isAlpha) str
36- first = read (takeWhile (isDigit) ((drop (length name + lbs)) str)) :: Integer
37- second = read ((init . tail . tail ) (dropWhile (/= ' .' ) str)) :: Integer
42+ first = read (takeWhile (isDigit) ((drop (length name + lbs)) str))
43+ second = read ((init . tail . tail ) (dropWhile (/= ' .' ) str))
3844 lbs = length " <-["
3945 domain = [first .. second]
4046
4147main :: IO ()
4248main = do input <- readInput
49+ putStr " solutions = "
4350 putStrLn (show (solveCSP input))
4451 return ()
0 commit comments