1
0
Fork 0
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.
 
 
 

19 lines
646 B

{-# 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