diff options
author | Akshay <[email protected]> | 2020-12-07 06:43:52 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2020-12-07 06:43:52 +0000 |
commit | 0dc33cafed11aa4574f740b244d96060008ec714 (patch) | |
tree | 220a41e99681bd52e87aa6dd53470d16f13d0c15 | |
parent | d7e77b3e9d8c9f30d34b46aac119521eb1589717 (diff) |
shorten
-rw-r--r-- | execs/Day07.hs | 11 | ||||
-rw-r--r-- | lib/Utils.hs | 4 |
2 files changed, 8 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 | ||
3 | import Data.Map (Map) | 3 | import Data.Map (Map) |
4 | import qualified Data.Map as Map | 4 | import qualified Data.Map as Map |
5 | import Utils | ||
5 | 6 | ||
6 | myBag = "shiny gold" | 7 | myBag = "shiny gold" |
7 | 8 | ||
@@ -19,18 +20,14 @@ parseLine s = (leadingBag, contained) | |||
19 | 20 | ||
20 | canContain :: Map String [(Int, String)] -> String -> Bool | 21 | canContain :: Map String [(Int, String)] -> String -> Bool |
21 | canContain m outer = myBag `elem` inners || any (canContain m) inners | 22 | canContain 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 | ||
24 | countNested :: String -> Map String [(Int, String)] -> Int | 25 | countNested :: String -> Map String [(Int, String)] -> Int |
25 | countNested s m = if null l | 26 | countNested 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 | ||
31 | main :: IO () | 28 | main :: IO () |
32 | main = do | 29 | main = 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 |
diff --git a/lib/Utils.hs b/lib/Utils.hs index 1eb42fc..4c9a581 100644 --- a/lib/Utils.hs +++ b/lib/Utils.hs | |||
@@ -4,6 +4,7 @@ module Utils ( binaryToInt | |||
4 | , right | 4 | , right |
5 | , bet | 5 | , bet |
6 | , (&+) | 6 | , (&+) |
7 | , howMany | ||
7 | ) where | 8 | ) where |
8 | 9 | ||
9 | import Data.Char (digitToInt) | 10 | import Data.Char (digitToInt) |
@@ -30,3 +31,6 @@ bet k (l, u) = k >= l && k <= u | |||
30 | -- combine filter predicates | 31 | -- combine filter predicates |
31 | (&+) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool) | 32 | (&+) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool) |
32 | (&+) = liftM2 (&&) | 33 | (&+) = liftM2 (&&) |
34 | |||
35 | howMany :: (a -> Bool) -> [a] -> Int | ||
36 | howMany predicate = length . filter ((== True) . predicate) | ||