Guest User

Untitled

a guest
Nov 19th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. data Paren = Open
  2. | Close
  3. deriving Show
  4.  
  5. toParenList :: String -> Maybe [Paren]
  6. toParenList [] = Just []
  7. toParenList (x:xs) = case x of
  8. '(' -> fmap (Open:) (toParenList xs)
  9. ')' -> fmap (Close:) (toParenList xs)
  10. _ -> Nothing
  11.  
  12. floors :: [Paren] -> [Int]
  13. floors = scanl (\acc y -> case y of
  14. Open -> acc + 1
  15. Close -> acc - 1
  16. ) 0
  17.  
  18. f3 :: String -> Maybe [Int]
  19. f3 = toParenList >=> pure . floors
  20.  
  21. floorEnds :: String -> Maybe Int
  22. floorEnds input2 = case f3 input2 of
  23. Just x -> Just $ last x
  24. Nothing -> Nothing
  25.  
  26. whenEntersBasement :: String -> Maybe Int
  27. whenEntersBasement input2 = case f3 input2 of
  28. Just x -> elemIndex (-1) x
  29. Nothing -> Nothing
  30.  
  31.  
  32. main' = fromMaybe "Invalid input" . fmap show . whenEntersBasement <$> readFile "input.txt"
Add Comment
Please, Sign In to add comment