Advent of Code solutions https://adventofcode.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

20 lines
646B

  1. {-# LANGUAGE OverloadedStrings #-}
  2. module Bag where
  3. import Data.Bifunctor
  4. import qualified Data.Text as T
  5. import Data.Tuple
  6. type Bag = (String, [(String, Int)])
  7. bagSpec :: String -> Bag
  8. bagSpec = bimap T.unpack contents . breakNoPrefix " bags contain " . T.pack
  9. contents :: T.Text -> [(String, Int)]
  10. contents c
  11. | c == "no other bags." = []
  12. | otherwise = map (bimap T.unpack (read . T.unpack) . swap . breakNoPrefix " " . T.strip . fst . T.breakOnEnd " ") . T.splitOn "," $ T.init c
  13. breakNoPrefix :: T.Text -> T.Text -> (T.Text, T.Text)
  14. breakNoPrefix prefix text = (a, T.drop (T.length prefix) b) where (a, b) = T.breakOn prefix text