diff --git a/day6.hs b/day6.hs new file mode 100644 index 0000000..e7df25a --- /dev/null +++ b/day6.hs @@ -0,0 +1,32 @@ +import Data.Foldable as Fold +import Data.Maybe as Maybe +import Data.List.Split +import Data.List as List + +main = do + content <- getContents + let ops = map parse $ lines content + let result = toTree [("COM",0)] ops + mapM putStrLn (map show result) + putStrLn $ show $ sum (map (\x-> snd x) result) + putStrLn("Hello World") + +toMap :: [(String,Int)] -> (String,String) -> [(String,Int)] +toMap state (p1,p2) = state ++ [(p2, (snd value) + 1)] + where value = (fromMaybe (p1,-1) ((Fold.find (\x -> fst x == p1) state))) + +toTree :: [(String,Int)] -> [(String,String)] -> [(String,Int)] +toTree dist ops = do + let visits = map fst dist + let newop = (Fold.find (\x-> (elem (fst x) visits) && (notElem (snd x) visits)) ops) + if Maybe.isNothing newop + then dist + else do + let newdist = toMap dist (Maybe.fromJust newop) + let newops = List.delete (Maybe.fromJust newop) ops + toTree newdist newops + +parse :: String -> (String, String) +parse input = do + let split = splitOn ")" input + (split !! 0, split !! 1)