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