Skip to content

Instantly share code, notes, and snippets.

@ChristophP
Last active January 14, 2018 22:33
Show Gist options
  • Save ChristophP/276a92fa13d59c111ab42fb6de256319 to your computer and use it in GitHub Desktop.
Save ChristophP/276a92fa13d59c111ab42fb6de256319 to your computer and use it in GitHub Desktop.
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