5 import qualified Data.Map
6 from Data.Map import :: Map(..), fromList, get, find, keys, foldrWithKey, findWithDefault
9 read :: *File -> [Char]
11 # (ok, l, f) = freadc f
17 # bag = fromList (makeBag (read io))
18 = (one ['shiny gold'] bag, two bag ['shiny gold']-1)
20 one color bag = length $ filter id $ map (canContain bag color) $ filter ((<>)color) $ keys bag
21 two bag color = 1+sum[n*two bag c\\(n, c)<-findWithDefault [] color bag]
23 makeBag :: [Char] -> [([Char], [(Int, [Char])])]
24 makeBag ls = [(b, mkItem c)\\[b,c]<-map (split [' contain ']) $ split ['.\n'] ls]
26 mkItem :: [Char] -> [(Int, [Char])]
27 mkItem s = [(toInt (toString x), join [' '] xs)\\[x:xs]<-map (split [' ']) (split [', '] s)]
29 canContain bag color name = or [name == color:[canContain bag color child\\(n, child)<-findWithDefault [] name bag]]