Last active
January 14, 2018 22:33
-
-
Save ChristophP/276a92fa13d59c111ab42fb6de256319 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List.Split | |
import Text.Read | |
main :: IO () | |
main = do | |
contents <- readFile "/home/deedo/Desktop/times.txt" | |
let line = getSecondLine $ splitOn "\n" contents | |
putStrLn $ prepareOutput $ calcDay <$> line | |
calcDay :: String -> Int | |
calcDay line = | |
case splitOn "," line of | |
[_, start, end, break] -> | |
totalMinutes $ toMinutes <$> [start, end, break] | |
_ -> 0 | |
totalMinutes:: [Int] -> Int | |
totalMinutes [start, end, break] = | |
end - start - break | |
toMinutes :: String -> Int | |
toMinutes time = | |
case readInt <$> splitOn ":" time of | |
[Just hours, Just minutes] -> | |
hours * 60 + minutes | |
_ -> 0 | |
getSecondLine :: [a] -> Maybe a | |
getSecondLine list = | |
case list of | |
_ : item : _ -> | |
Just item | |
_ -> | |
Nothing | |
minutesToFormattedTime :: Int -> String | |
minutesToFormattedTime minutes = | |
let h = minutes `quot` 60 | |
m = minutes - (h * 60) | |
in | |
show h ++ ":" ++ show m ++ " h" | |
readInt:: String -> Maybe Int | |
readInt = readMaybe | |
prepareOutput :: Maybe Int -> String | |
prepareOutput maybeInt = | |
case maybeInt of | |
Just time -> | |
minutesToFormattedTime time | |
Nothing -> | |
"Could read correctly from the file." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment