Improvements for day 1 and day 2
This commit is contained in:
@@ -1,34 +1,34 @@
|
||||
module Commons where
|
||||
|
||||
import Data.String.Utils
|
||||
import System.IO
|
||||
import Text.Read
|
||||
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 :: [[String]] -> Round -> Round
|
||||
parseCubes [] round = round
|
||||
parseCubes ([h, ' ', 'r', 'e', 'd']: t) round = parseCubes t round {red = read [h]}
|
||||
parseCubes ([h1, h2, ' ', 'r', 'e', 'd']: t) round = parseCubes t round {red = read [h1, h2]}
|
||||
parseCubes ([h, ' ', 'g', 'r', 'e', 'e', 'n']: t) round = parseCubes t round {green = read [h]}
|
||||
parseCubes ([h1, h2, ' ', 'g', 'r', 'e', 'e', 'n']: t) round = parseCubes t round {green = read [h1, h2]}
|
||||
parseCubes ([h, ' ', 'b', 'l', 'u', 'e']: t) round = parseCubes t round {blue = read [h]}
|
||||
parseCubes ([h1, h2, ' ', 'b', 'l', 'u', 'e']: t) round = parseCubes t round {blue = read [h1, h2]}
|
||||
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 (split ", " roundRaw) Round {red = 0, green = 0, blue = 0}
|
||||
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 ('G': 'a': 'm': 'e': ' ': h: ':': ' ': t) = Game {gid = read [h], rounds = parseRounds t}
|
||||
parseGame ('G': 'a': 'm': 'e': ' ': h1: h2: ':': ' ': t) = Game {gid = read [h1, h2], rounds = parseRounds t}
|
||||
parseGame ('G': 'a': 'm': 'e': ' ': h1: h2: h3: ':': ' ': t) = Game {gid = read [h1, h2, h3], rounds = parseRounds t}
|
||||
parseGame (_: t) = parseGame t
|
||||
parseGame line = let (h: t: _) = split ": " line
|
||||
in Game {gid = read (parseGameId h), rounds = parseRounds t}
|
||||
|
||||
parse :: IO [Game]
|
||||
parse = do done <- isEOF
|
||||
|
||||
Reference in New Issue
Block a user