From: Mart Lubbers Date: Mon, 7 Dec 2020 06:27:55 +0000 (+0100) Subject: make a bit better X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=c63db55cfbfa0c62a5844691c47accbaf27dcdc7;p=aoc20.git make a bit better --- diff --git a/7/one.icl b/7/one.icl index d1b2fba..0c3b8a1 100644 --- a/7/one.icl +++ b/7/one.icl @@ -2,10 +2,12 @@ module one import StdEnv import Data.Func -import qualified Data.Map -from Data.Map import :: Map(..), fromList, get, find, keys, foldrWithKey, findWithDefault +import Data.Map => qualified updateAt import Text + +//A bit cheaty because I removed all occurances of bag and bags from the input + read :: *File -> [Char] read f # (ok, l, f) = freadc f @@ -15,15 +17,17 @@ read f Start w # (io, w) = stdio w # bag = fromList (makeBag (read io)) - = (one ['shiny gold'] bag, two bag ['shiny gold']-1) + = (one ['shiny gold'] bag, dec $ two ['shiny gold'] bag) -one color bag = length $ filter id $ map (canContain bag color) $ filter ((<>)color) $ keys bag -two bag color = 1+sum[n*two bag c\\(n, c)<-findWithDefault [] color bag] +one color bag = dec $ length $ filter id $ map (canContain bag color) $ keys bag +two color bag = inc $ sum [n*two c bag\\(n, c)<-findWithDefault [] color bag] makeBag :: [Char] -> [([Char], [(Int, [Char])])] makeBag ls = [(b, mkItem c)\\[b,c]<-map (split [' contain ']) $ split ['.\n'] ls] where mkItem :: [Char] -> [(Int, [Char])] - mkItem s = [(toInt (toString x), join [' '] xs)\\[x:xs]<-map (split [' ']) (split [', '] s)] + mkItem s = [(toInt (toString x), join [' '] xs)\\[x:xs]<-map (split [' ']) $ split [', '] s] -canContain bag color name = or [name == color:[canContain bag color child\\(n, child)<-findWithDefault [] name bag]] +canContain :: (Map [Char] [(Int,[Char])]) [Char] [Char] -> Bool +canContain bag color name + = or [name == color:map (canContain bag color o snd) $ findWithDefault [] name bag]