2020-12-07 13:38:45 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2020-12-07 12:29:36 +00:00
|
|
|
module Bag where
|
|
|
|
|
2020-12-07 13:38:45 +00:00
|
|
|
import Data.Bifunctor
|
2020-12-07 12:29:36 +00:00
|
|
|
import qualified Data.Text as T
|
2020-12-07 13:38:45 +00:00
|
|
|
import Data.Tuple
|
2020-12-07 12:29:36 +00:00
|
|
|
|
|
|
|
type Bag = (String, [(String, Int)])
|
|
|
|
|
|
|
|
bagSpec :: String -> Bag
|
2020-12-07 13:38:45 +00:00
|
|
|
bagSpec = bimap T.unpack contents . breakNoPrefix " bags contain " . T.pack
|
2020-12-07 12:29:36 +00:00
|
|
|
|
|
|
|
contents :: T.Text -> [(String, Int)]
|
|
|
|
contents c
|
2020-12-07 13:38:45 +00:00
|
|
|
| c == "no other bags." = []
|
|
|
|
| otherwise = map (bimap T.unpack (read . T.unpack) . swap . breakNoPrefix " " . T.strip . fst . T.breakOnEnd " ") . T.splitOn "," $ T.init c
|
2020-12-07 12:29:36 +00:00
|
|
|
|
|
|
|
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
|