41 lines
1.3 KiB
Haskell
41 lines
1.3 KiB
Haskell
module Commons where
|
|
|
|
import Data.List.Utils (split)
|
|
import GHC.IO.Handle (isEOF)
|
|
import Data.List (isSuffixOf)
|
|
|
|
|
|
data Round = Round { red :: Int, green :: Int, blue :: Int }
|
|
data Game = Game { gid :: Int, rounds :: [Round] }
|
|
|
|
|
|
parseCubes :: [[String]] -> Round -> Round
|
|
parseCubes [] round = round
|
|
parseCubes ([h, "red"]: t) round = parseCubes t round {red = read h}
|
|
parseCubes ([h, "green"]: t) round = parseCubes t round {green = read h}
|
|
parseCubes ([h, "blue"]: t) round = parseCubes t round {blue = read h}
|
|
|
|
parseRound :: String -> Round
|
|
parseRound roundRaw = parseCubes (map words (split ", " roundRaw)) Round {red = 0, green = 0, blue = 0}
|
|
|
|
parseRounds :: String -> [Round]
|
|
parseRounds roundsRaw = map parseRound (split "; " roundsRaw)
|
|
|
|
parseGameId :: String -> String
|
|
parseGameId ('G': 'a': 'm': 'e': ' ': t) = parseGameId t
|
|
parseGameId (h: t) = h: parseGameId t
|
|
parseGameId [] = []
|
|
|
|
parseGame :: String -> Game
|
|
parseGame line = let (h: t: _) = split ": " line
|
|
in Game {gid = read (parseGameId h), rounds = parseRounds t}
|
|
|
|
parse :: IO [Game]
|
|
parse = do done <- isEOF
|
|
if done
|
|
then return []
|
|
else do line <- getLine
|
|
let game = parseGame line
|
|
otherGames <- parse
|
|
return (game: otherGames)
|