diff options
-rw-r--r-- | Puzzle7.hs | 56 |
1 files changed, 56 insertions, 0 deletions
@@ -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) @@ -19,6 +21,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)" |