From f2f0df9142ec3d74ef448cdcc28829eb4b19acb4 Mon Sep 17 00:00:00 2001 From: Arranun Date: Sat, 14 Dec 2019 19:03:13 +0100 Subject: [PATCH] Day14: Both parts work .... somehow --- day14.hs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/day14.hs b/day14.hs index e4a96e6..b70fec4 100644 --- a/day14.hs +++ b/day14.hs @@ -7,12 +7,9 @@ import Debug.Trace as T main = do reactions <- map getReaction <$> lines <$> getContents let amout = getConstruction reactions (1,"A") - let ore = getOre [(1,"FUEL")] [] reactions - let result = getRestOre ore [] reactions let test = part1 [(1,"FUEL")] [] reactions putStrLn(show $ head reactions) --putStrLn(show ore) - putStrLn(show result) putStrLn(show test) --putStrLn(show sumResult1) data Reaction = Reaction { input :: [(Int, String)], @@ -51,10 +48,10 @@ getConstructionRest reactions (amount, elem) part1 :: [(Int,String)] -> [(Int,String)] -> [Reaction] -> [(Int,String)] part1 needs oldNeeds reactions - |needs == oldNeeds = needs + | needs == oldNeeds = needs | otherwise = part1 newNeeds needs reactions - where newNeeds = getRestOre fullNeeds [] reactions - fullNeeds = getOre needs [] reactions + where newNeeds = getRestOre (T.traceShowId(fullNeeds)) [] reactions + fullNeeds = getOre (T.traceShowId(needs)) [] reactions getOre :: [(Int,String)] -> [(Int,String)] -> [Reaction] -> [(Int,String)] getOre needs oldNeeds reactions @@ -66,9 +63,9 @@ getOre needs oldNeeds reactions getRestOre :: [(Int,String)] -> [(Int,String)] -> [Reaction] -> [(Int,String)] getRestOre needs oldNeeds reactions = sumNeeds - where pureNeeds = getPureNeeds needs reactions - pureRest = foldl (++)[] (map(\(amount,xs) -> map(\(a,e) -> ((amount * a),e)) xs) (map (getConstructionRest reactions) pureNeeds)) - sumNeeds = foldl combineNeeds [] ((needs \\ pureNeeds) ++ pureRest) + where pureNeeds = (T.traceShowId(head ( getPureNeeds needs reactions))) + pureRest = foldl (++)[] (map(\(amount,xs) -> map(\(a,e) -> ((amount * a),e)) xs) (map (getConstructionRest reactions) [pureNeeds])) + sumNeeds = foldl combineNeeds [] ((needs \\ [pureNeeds]) ++ pureRest) combineNeeds :: [(Int,String)] -> (Int,String) -> [(Int,String)] combineNeeds xs (amt, elem) @@ -77,7 +74,7 @@ combineNeeds xs (amt, elem) where oldVal = filter(\(a,e) -> e == elem) xs getPureNeeds :: [(Int,String)] -> [Reaction] -> [(Int,String)] -getPureNeeds needs reactions = filter(\(a,e) -> notElem e impureElements ) needs +getPureNeeds needs reactions = filter(\(a,e) -> notElem e (impureElements) ) needs where impureElements = map(\(a,e) -> e) ( (foldl (++) [] ( map(\(Reaction i o) -> i)reactionList ))) reactionList = (filter(\(Reaction i o) -> elem (snd o) elements) reactions) elements = (map(\(a,e) -> e) needs)