From 4d486b471c9ac8943b5bd6595b4544d2d07d95a6 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Sat, 9 Dec 2017 21:49:43 +0100 Subject: attempted part 2 of puzzle 7 --- Puzzle7.hs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/Puzzle7.hs b/Puzzle7.hs index 9010dd0..00dae27 100644 --- a/Puzzle7.hs +++ b/Puzzle7.hs @@ -1,5 +1,7 @@ import Data.Set (toList, (\\), fromList) import Data.List.Split (splitOn, splitOneOf) +import Data.Map (Map) +import qualified Data.Map as Map parseInput :: [Char] -> [[Char]] parseInput xs = filter (elem '>') (splitOn "\n" xs) @@ -18,6 +20,60 @@ solve1FromPair (xs, ys) = head $ toList $ (fromList xs) \\ (fromList ys) solve1 :: [Char] -> [Char] solve1 = solve1FromPair . g . f . parseInput + +parseLine xs = (node, (weight, children)) + where node:weight:children = splitOneOf ",()-> " xs + +parseInput' xs = Map.fromList . (fmap parseLine) . (splitOn "\n") xs + +update nodes weights node + | null children = Map.insert node weight weights + | otherwise = fromJust . Map.lookup $ foldl (newWeights nodes) weights children + where (weight, children) = fromJust . Map.lookup node nodes + +newWeights nodes weights node = + if member node weights + then weights + else update nodes weights node + +findAnomaly xs + | null ys = Nothing + | length ys > 1 = Just $ fst $ head xs + | otherwise = Just $ fst $ head ys + where ys = filter (\z -> snd z /= snd head xs) xs + + +solve2' nodes node weight + fst $ until (isNothing . snd) badChild (root, Just root) + where + root = solve1 input + badChild x + | isNothing $ snd x = x + | otherwise = findAnomaly (\z -> (z, fromJust $ Map.lookup z weights)) <$> (snd . fromJust . Map.lookup y) nodes + where y = fromJust . snd x + + + + + + + + + + + + + + + + + + + + + + + input0 = "pbga (66)\nxhth (57)\nebii (61)\nhavc (66)\nktlj (57)\nfwft (72) -> ktlj, cntj, xhth\nqoyq (66)\npadx (45) -> pbga, havc, qoyq\ntknk (41) -> ugml, padx, fwft\njptl (61)\nugml (68) -> gyxo, ebii, jptl\ngyxo (61)\ncntj (57)" -- cgit v1.2.3