Skip to content

Commit 24f889b

Browse files
committed
finished problem 7
1 parent 9fb59f3 commit 24f889b

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

7.hs

+42-21
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,18 @@ dummyInput = ["light red bags contain 1 bright white bag, 2 muted yellow bags."
1919
,"faded blue bags contain no other bags."
2020
,"dotted black bags contain no other bags."]
2121

22-
-- not working yet; not sure why
22+
dummy2 = [ "shiny gold bags contain 2 dark red bags."
23+
, "dark red bags contain 2 dark orange bags."
24+
, "dark orange bags contain 2 dark yellow bags."
25+
, "dark yellow bags contain 2 dark green bags."
26+
, "dark green bags contain 2 dark blue bags."
27+
, "dark blue bags contain 2 dark violet bags."
28+
, "dark violet bags contain no other bags." ]
29+
30+
2331
main = do
2432
let color = "shiny gold"
33+
rule = getFirst (\(Rule x _) -> x == color)
2534
putStrLn $ "possible bags to contain a shiny gold bag in the input:\n" ++
2635
(show $ map bag $ getAllBagRules color $ map (fromJust . parseRule) dummyInput)
2736

@@ -33,6 +42,18 @@ main = do
3342
--putStrLn $ "possible bag colors: " ++ (show $ colors)
3443
putStrLn $ "possible bag colors: " ++ (show $ length colors)
3544

45+
---
46+
let toRules = map (fromJust . parseRule)
47+
dummyRules = toRules dummyInput
48+
dummy2Rules = toRules dummy2
49+
inputRules = map fromJust rules
50+
putStrLn $ "numberOfBags in dummy inpuy: " ++
51+
(show $ getContainedBags (rule $ dummyRules) dummyRules)
52+
putStrLn $ "numberOfBags in dummy 2: " ++
53+
(show $ getContainedBags (rule $ dummy2Rules) dummy2Rules)
54+
putStrLn $ "numberOfBags in input: " ++
55+
(show $ getContainedBags (rule $ inputRules) inputRules)
56+
3657
type Bag = String
3758

3859
data Rule = Rule { bag :: !Bag
@@ -68,26 +89,6 @@ parseRule s = let
6889
trace' :: Show a => a -> a
6990
trace' !a = trace (show a) a
7091

71-
{-
72-
test rules = fix (\b -> sort
73-
$ distinct
74-
$ intercalate []
75-
$ map (`searchForBagRules` rules) b)
76-
-}
77-
{-
78-
79-
searchAllBags :: Bag -> [Rule] -> [Bag]
80-
searchAllBags bag rules = fix (\rec bs ->
81-
let x = sort
82-
$ distinct
83-
$ (++bs)
84-
$ intercalate []
85-
$ map (\x -> searchForBagDirect x rules)
86-
$ filter (/=bag) bs
87-
in if bs == x then bs else rec x) (searchForBagDirect bag rules)
88-
--(searchForBagDirect bag rules)
89-
-}
90-
9192

9293
getAllBagRules :: Bag -> [Rule] -> [Rule]
9394
getAllBagRules b r = fix
@@ -97,8 +98,28 @@ getAllBagRules b r = fix
9798
$ map (\(Rule x _) -> searchForBagRules x r) n
9899
in if n == comb then comb else rec comb) $ searchForBagRules b r
99100

101+
getFirst :: (a -> Bool) -> [a] -> a
102+
getFirst f = head . filter f
103+
104+
getContainedBags :: Rule -> [Rule] -> Int
105+
getContainedBags b r = fix
106+
(\rec (Rule n bs) ->
107+
let res = sum
108+
$ map (\(b', n) ->
109+
n * (1+ rec (getFirst (\(Rule x _) -> x == b') r))) bs
110+
in res)
111+
$ b
112+
113+
searchForBags :: Bag -> [Rule] -> [(Bag, Int)]
114+
searchForBags b bs = firstInstances
115+
where
116+
getBagsFor x bs = filter (\(Rule b' _) -> x == b') bs
117+
firstInstances = intercalate [] $ map spec $ getBagsFor b bs
118+
100119
searchForBagRules :: Bag -> [Rule] -> [Rule]
101120
searchForBagRules b bs = firstInstances
102121
where
103122
getRulesFor x bs = filter (\(Rule _ bs') -> elem x $ map fst bs') $ filter ((/=x) . bag) bs
104123
firstInstances = getRulesFor b bs
124+
125+

0 commit comments

Comments
 (0)