aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-12-07 07:08:50 +0000
committerAkshay <[email protected]>2020-12-07 07:08:50 +0000
commit7c26160075af3ccd87e0029ecc0e09ad3ca2dc66 (patch)
treef836453db8b62f5304d3fdb13f66df5939771b0d
parent0dc33cafed11aa4574f740b244d96060008ec714 (diff)
shorten day07
-rw-r--r--execs/Day07.hs8
-rw-r--r--lib/Utils.hs2
2 files changed, 4 insertions, 6 deletions
diff --git a/execs/Day07.hs b/execs/Day07.hs
index 72043af..a0c1eb0 100644
--- a/execs/Day07.hs
+++ b/execs/Day07.hs
@@ -8,15 +8,13 @@ myBag = "shiny gold"
8 8
9parseContained :: [String] -> [(Int, String)] 9parseContained :: [String] -> [(Int, String)]
10parseContained [] = [] 10parseContained [] = []
11parseContained ("no":_) = []
11parseContained (count:b:c:_:rest) = (read count, unwords [b,c]):parseContained rest 12parseContained (count:b:c:_:rest) = (read count, unwords [b,c]):parseContained rest
12 13
13parseLine :: [String] -> (String, [(Int, String)]) 14parseLine :: [String] -> (String, [(Int, String)])
14parseLine s = (leadingBag, contained) 15parseLine s = (leadingBag, parseContained trailingBags)
15 where leadingBag = unwords $ take 2 s 16 where leadingBag = unwords (take 2 s)
16 trailingBags = drop 4 s 17 trailingBags = drop 4 s
17 contained = case head trailingBags of
18 "no" -> []
19 _ -> parseContained trailingBags
20 18
21canContain :: Map String [(Int, String)] -> String -> Bool 19canContain :: Map String [(Int, String)] -> String -> Bool
22canContain m outer = myBag `elem` inners || any (canContain m) inners 20canContain m outer = myBag `elem` inners || any (canContain m) inners
diff --git a/lib/Utils.hs b/lib/Utils.hs
index 4c9a581..4319714 100644
--- a/lib/Utils.hs
+++ b/lib/Utils.hs
@@ -33,4 +33,4 @@ bet k (l, u) = k >= l && k <= u
33(&+) = liftM2 (&&) 33(&+) = liftM2 (&&)
34 34
35howMany :: (a -> Bool) -> [a] -> Int 35howMany :: (a -> Bool) -> [a] -> Int
36howMany predicate = length . filter ((== True) . predicate) 36howMany predicate = length . filter predicate