aoc

advent of code
git clone git://source.orangerot.dev:/aoc.git
Log | Files | Refs

MySplit.hs (989B)


      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 main :: IO ()
     32 main = do
     33   inputLines <- lines <$> getContents
     34 
     35   -- let arePossible = filter isPossible lines
     36   -- myColors <- isValid inputLines
     37   print (sum (map (\(_,n) -> n) (filter (\(b,_) -> b) (zip (map isGameValid inputLines) [1..]))))
     38 
     39   -- mapM_ putStrLn myColors
     40   -- mapM_ putStrLn inputLines
     41