Day14: Both parts work .... somehow

This commit is contained in:
Arranun 2019-12-14 19:03:13 +01:00
parent 9da7f98590
commit f2f0df9142

View File

@ -7,12 +7,9 @@ import Debug.Trace as T
main = do main = do
reactions <- map getReaction <$> lines <$> getContents reactions <- map getReaction <$> lines <$> getContents
let amout = getConstruction reactions (1,"A") let amout = getConstruction reactions (1,"A")
let ore = getOre [(1,"FUEL")] [] reactions
let result = getRestOre ore [] reactions
let test = part1 [(1,"FUEL")] [] reactions let test = part1 [(1,"FUEL")] [] reactions
putStrLn(show $ head reactions) putStrLn(show $ head reactions)
--putStrLn(show ore) --putStrLn(show ore)
putStrLn(show result)
putStrLn(show test) putStrLn(show test)
--putStrLn(show sumResult1) --putStrLn(show sumResult1)
data Reaction = Reaction { input :: [(Int, String)], data Reaction = Reaction { input :: [(Int, String)],
@ -51,10 +48,10 @@ getConstructionRest reactions (amount, elem)
part1 :: [(Int,String)] -> [(Int,String)] -> [Reaction] -> [(Int,String)] part1 :: [(Int,String)] -> [(Int,String)] -> [Reaction] -> [(Int,String)]
part1 needs oldNeeds reactions part1 needs oldNeeds reactions
|needs == oldNeeds = needs | needs == oldNeeds = needs
| otherwise = part1 newNeeds needs reactions | otherwise = part1 newNeeds needs reactions
where newNeeds = getRestOre fullNeeds [] reactions where newNeeds = getRestOre (T.traceShowId(fullNeeds)) [] reactions
fullNeeds = getOre needs [] reactions fullNeeds = getOre (T.traceShowId(needs)) [] reactions
getOre :: [(Int,String)] -> [(Int,String)] -> [Reaction] -> [(Int,String)] getOre :: [(Int,String)] -> [(Int,String)] -> [Reaction] -> [(Int,String)]
getOre needs oldNeeds reactions getOre needs oldNeeds reactions
@ -66,9 +63,9 @@ getOre needs oldNeeds reactions
getRestOre :: [(Int,String)] -> [(Int,String)] -> [Reaction] -> [(Int,String)] getRestOre :: [(Int,String)] -> [(Int,String)] -> [Reaction] -> [(Int,String)]
getRestOre needs oldNeeds reactions = sumNeeds getRestOre needs oldNeeds reactions = sumNeeds
where pureNeeds = getPureNeeds needs reactions where pureNeeds = (T.traceShowId(head ( getPureNeeds needs reactions)))
pureRest = foldl (++)[] (map(\(amount,xs) -> map(\(a,e) -> ((amount * a),e)) xs) (map (getConstructionRest reactions) pureNeeds)) pureRest = foldl (++)[] (map(\(amount,xs) -> map(\(a,e) -> ((amount * a),e)) xs) (map (getConstructionRest reactions) [pureNeeds]))
sumNeeds = foldl combineNeeds [] ((needs \\ pureNeeds) ++ pureRest) sumNeeds = foldl combineNeeds [] ((needs \\ [pureNeeds]) ++ pureRest)
combineNeeds :: [(Int,String)] -> (Int,String) -> [(Int,String)] combineNeeds :: [(Int,String)] -> (Int,String) -> [(Int,String)]
combineNeeds xs (amt, elem) combineNeeds xs (amt, elem)
@ -77,7 +74,7 @@ combineNeeds xs (amt, elem)
where oldVal = filter(\(a,e) -> e == elem) xs where oldVal = filter(\(a,e) -> e == elem) xs
getPureNeeds :: [(Int,String)] -> [Reaction] -> [(Int,String)] 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 ))) where impureElements = map(\(a,e) -> e) ( (foldl (++) [] ( map(\(Reaction i o) -> i)reactionList )))
reactionList = (filter(\(Reaction i o) -> elem (snd o) elements) reactions) reactionList = (filter(\(Reaction i o) -> elem (snd o) elements) reactions)
elements = (map(\(a,e) -> e) needs) elements = (map(\(a,e) -> e) needs)