aboutsummaryrefslogtreecommitdiff
path: root/execs
diff options
context:
space:
mode:
Diffstat (limited to 'execs')
-rw-r--r--execs/Day07.hs11
1 files changed, 4 insertions, 7 deletions
diff --git a/execs/Day07.hs b/execs/Day07.hs
index 1587e2b..72043af 100644
--- a/execs/Day07.hs
+++ b/execs/Day07.hs
@@ -2,6 +2,7 @@ module Main where
2 2
3import Data.Map (Map) 3import Data.Map (Map)
4import qualified Data.Map as Map 4import qualified Data.Map as Map
5import Utils
5 6
6myBag = "shiny gold" 7myBag = "shiny gold"
7 8
@@ -19,18 +20,14 @@ parseLine s = (leadingBag, contained)
19 20
20canContain :: Map String [(Int, String)] -> String -> Bool 21canContain :: Map String [(Int, String)] -> String -> Bool
21canContain m outer = myBag `elem` inners || any (canContain m) inners 22canContain m outer = myBag `elem` inners || any (canContain m) inners
22 where (Just inners) = map snd <$> Map.lookup outer m 23 where inners = map snd $ m Map.! outer
23 24
24countNested :: String -> Map String [(Int, String)] -> Int 25countNested :: String -> Map String [(Int, String)] -> Int
25countNested s m = if null l 26countNested s m = foldl (\acc (c,b) -> acc + c * (1 + countNested b m)) 0 $ m Map.! s
26 then 0
27 else sum $ map counter l
28 where (Just l) = Map.lookup s m
29 counter (count, bag) = count + count * countNested bag m
30 27
31main :: IO () 28main :: IO ()
32main = do 29main = do
33 n <- map (parseLine . words) . lines <$> readFile "input/07" 30 n <- map (parseLine . words) . lines <$> readFile "input/07"
34 let q = Map.fromList n 31 let q = Map.fromList n
35 print $ length $ filter (== True) $ map (canContain q . fst) $ Map.toList q 32 print $ howMany (canContain q . fst) $ Map.toList q
36 print $ countNested myBag q 33 print $ countNested myBag q