Created
April 4, 2015 20:57
-
-
Save tom-galvin/156a4976f2cdc6254db8 to your computer and use it in GitHub Desktop.
DailyProgrammer Challenge #209e Solution (The Button can be pressed but once...)
This file contains hidden or 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 Control.Monad | |
import Data.Char | |
import Data.List | |
import Data.Ord | |
import System.IO | |
type Time = Double | |
type User = (String, Time) | |
readUser :: String -> User | |
readUser s = (userName, read $ dropWhile (not . isDigit) time) | |
where (userName, time) = break (== ':') s | |
readUsers :: [User] -> Int -> IO [User] | |
readUsers users 0 = return $ sortBy (comparing snd) $ users | |
readUsers users n = getLine >>= \user -> readUsers (readUser user : users) (n - 1) | |
timeUser :: Time -> User -> (Time, User) | |
timeUser time (userName, absoluteTime) = (absoluteTime, (userName, 60.0 + time - absoluteTime)) | |
main = do numUsers <- getLine | |
users <- readUsers [] $ read numUsers | |
putStr | |
$ unlines | |
$ map (\(userName, time) -> userName ++ ": " ++ (show $ floor time)) $ snd | |
$ mapAccumL timeUser 0.0 | |
$ users |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment