MySplit2.hs (1368B)
1 import Data.List.Split (splitOn) 2 3 game :: String -> [String] 4 game = splitOn [';'] 5 6 reveal :: String -> [String] 7 reveal x = concat ( map (splitOn [',']) (game x) ) 8 9 colors :: String -> [(Int, String)] 10 colors x = map (\x -> color (splitOn [' '] x)) (reveal x) 11 12 color :: [String] -> (Int, String) 13 color [_, i, s] = (read i, s) 14 15 validColor :: (Int, String) -> Bool 16 validColor (i, s) = case s of 17 "red" -> i <= 12 18 "green" -> i <= 13 19 "blue" -> i <= 14 20 _ -> False 21 22 getColors :: String -> String 23 getColors s = tail (dropWhile (/= ':') s) 24 25 isGameValid :: String -> Bool 26 isGameValid s = all validColor (colors (getColors s)) 27 28 isValid :: [String] -> Bool 29 isValid s = all isGameValid s 30 31 getMyColor :: String -> [(Int, String)] 32 getMyColor s = colors (getColors s) 33 34 myFilter :: String -> [(Int, String)] -> Int 35 myFilter color s = maximum (map (\(i,_) -> i) (filter (\(_,c) -> c == color) s)) 36 37 main :: IO () 38 main = do 39 inputLines <- lines <$> getContents 40 41 -- let arePossible = filter isPossible lines 42 -- myColors <- isValid inputLines 43 -- print (sum (map (\(_,n) -> n) (filter (\(b,_) -> b) (zip (map isGameValid inputLines) [1..])))) 44 let myColors = map (\x -> getMyColor x) inputLines 45 let myResult = map (\c -> myFilter "red" c * myFilter "green" c * myFilter "blue" c) myColors 46 print (sum myResult) 47 48 -- mapM_ putStrLn myColors 49 -- mapM_ putStrLn inputLines 50