toHHMM :: String -> (Int, Int) toHHMM s = ((read :: String -> Int) . takeWhile (/= ':') $ s, (read :: String -> Int) . tail . dropWhile (/= ':') $ s) sleepTime :: (Int, Int) -> (Int, Int) -> Int sleepTime (h1, m1) (h2, m2) | h2 > h1 && m2 >= m1 = (h2 - h1) * 60 + m2 - m1 | h2 > h1 && m2 < m1 = (h2 - 1 - h1) * 60 + m2 + 60 - m1 | h2 == h1 && m2 >= m1 = m2 - m1 | otherwise = (h2 + 23 - h1) * 60 + m2 + 60 - m1 splitAt2 :: [String] -> [[String]] splitAt2 = takeWhile (not.null) . map (take 2) . iterate (drop 2) solve :: [[String]] -> Int solve xss = sum zs where ys = map (map toHHMM) xss zs = map (\[x, y] -> sleepTime x y) ys main :: IO () main = interact $ show . solve . splitAt2 . tail . words