Day 7, part 2 - partial progress: made a tree, I guess?

This commit is contained in:
Jonathan Chan 2017-12-06 23:22:16 -08:00
parent 91d0fb46bc
commit 6410560315
2 changed files with 2218 additions and 11 deletions

34
7.hs
View File

@ -1,5 +1,6 @@
import Data.HashSet (Set, fromList, delete)
import Data.HashMap (Map, fromList, toList, delete, findWithDefault)
import Data.List.Split
import Data.Tree (Tree, unfoldTree, drawTree)
type Weight = Int
type Program = String
@ -9,21 +10,32 @@ discardEmpty :: [String] -> [String]
discardEmpty [""] = []
discardEmpty xs = xs
parseLine :: String -> (Program, Weight, Programs)
parseLine :: String -> (Program, (Weight, Programs))
parseLine line =
let nameAndWeight : programsString : _ = splitOn ")" line
programs = discardEmpty $ splitOn ", " $ last $ splitOn " -> " programsString
name : weight : _ = splitOn " (" nameAndWeight
in (name, read weight, programs)
in (name, (read weight, programs))
getBottom :: Map Program (Weight, Programs) -> [(Program, (Weight, Programs))] -> Program
getBottom m l = bottomName
where
(bottomName, _) : _ = toList $ foldr
(\(name, (_, programs)) set ->
case programs of
[] -> delete name set
ps -> foldr delete set ps)
m l
mapToTree :: Map Program (Weight, Programs) -> Program -> Tree Weight
mapToTree m = unfoldTree (\s -> findWithDefault undefined s m)
main :: IO ()
main = do
input <- readFile "7.txt"
let list = map parseLine $ lines input
nameSet = fromList $ map (\(name, _, _) -> name) list
bottom = foldr (\(name, _, programs) set ->
case programs of
[] -> delete name set
ps -> foldr delete set ps)
nameSet list
print $ bottom
let programsList = map parseLine $ lines input
programsMap = fromList programsList
bottomName = getBottom programsMap programsList
programsTree = mapToTree programsMap bottomName
print $ bottomName
writeFile "7_tree.txt" $ drawTree $ fmap show programsTree

2195
7_tree.txt Normal file

File diff suppressed because it is too large Load Diff