aoc

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

Main2.hs (835B)


      1 import Data.List.Split (splitOn)
      2 
      3 getGame :: String -> String
      4 getGame s = tail $ dropWhile (/= ':') s
      5 
      6 splitGame :: String -> [[String]]
      7 splitGame s = map (\x -> filter (/= "") (splitOn [' '] x)) (splitOn ['|'] s)
      8 
      9 calcPoints :: [(Int, Int)] -> (Int, [(Int, Int)]) -> (Int, [(Int, Int)])
     10 calcPoints dict (num_cards, []) = (num_cards, [])
     11 calcPoints dict (num_cards, this) = 
     12   let new_wins = map (\(i, w) -> take w (drop i dict)) this 
     13   in calcPoints dict (num_cards + length new_wins, concat new_wins)
     14 
     15 
     16 
     17 main :: IO ()
     18 main = do
     19   inputLines <- lines <$> getContents
     20   let games = map (\x -> splitGame (getGame x)) inputLines
     21   let wins = zip [1..] (map (\[w, cards] -> length (filter (\card -> elem card w) cards)) games) :: [(Int, Int)]
     22   -- let points = map calcPoints wins
     23 
     24   print (calcPoints wins (0, wins))
     25   -- print (wins)
     26