1
0
Fork 0

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.List.Split
import Data.Tree (Tree, unfoldTree, drawTree)
type Weight = Int type Weight = Int
type Program = String type Program = String
@ -9,21 +10,32 @@ discardEmpty :: [String] -> [String]
discardEmpty [""] = [] discardEmpty [""] = []
discardEmpty xs = xs discardEmpty xs = xs
parseLine :: String -> (Program, Weight, Programs) parseLine :: String -> (Program, (Weight, Programs))
parseLine line = parseLine line =
let nameAndWeight : programsString : _ = splitOn ")" line let nameAndWeight : programsString : _ = splitOn ")" line
programs = discardEmpty $ splitOn ", " $ last $ splitOn " -> " programsString programs = discardEmpty $ splitOn ", " $ last $ splitOn " -> " programsString
name : weight : _ = splitOn " (" nameAndWeight 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 :: IO ()
main = do main = do
input <- readFile "7.txt" input <- readFile "7.txt"
let list = map parseLine $ lines input let programsList = map parseLine $ lines input
nameSet = fromList $ map (\(name, _, _) -> name) list programsMap = fromList programsList
bottom = foldr (\(name, _, programs) set -> bottomName = getBottom programsMap programsList
case programs of programsTree = mapToTree programsMap bottomName
[] -> delete name set print $ bottomName
ps -> foldr delete set ps) writeFile "7_tree.txt" $ drawTree $ fmap show programsTree
nameSet list
print $ bottom

2195
7_tree.txt Normal file

File diff suppressed because it is too large Load Diff