Successful solution to 7.1.

This commit is contained in:
Anna Rose 2018-12-10 00:29:13 -05:00
parent f6ea56d266
commit 7f0bc4d9f1
No known key found for this signature in database
GPG Key ID: 8D9ACA841015C59A
2 changed files with 36 additions and 42 deletions

View File

@ -1,51 +1,46 @@
package tree package tree
import "fmt" // debug
// FindOrder determines the correct order according to these rules: // FindOrder determines the correct order according to these rules:
// 1. A node cannot come before all of its parents have been included. // 1. A node cannot come before all of its parents have been included.
// 2. At any given time, the lowest-lettered step must be executed. // 2. At any given time, the lowest-lettered available step must be executed.
func FindOrder(root *Node) []*Node { func FindOrder(root *Node) []*Node {
seen := make(map[rune]bool) seen := map[rune]bool{
return findOrderR(root, seen) 0: true,
} }
func findOrderR(node *Node, seen map[rune]bool) []*Node {
order := []*Node{} order := []*Node{}
// debug for {
fmt.Printf("In node: %c | Children: ", node.Name) // continuously loop over the tree, retrieving the next usable node.
candidates := findCandidates(root, seen)
if len(candidates) == 0 {
return order
}
sortNodes(candidates)
order = append(order, candidates[0])
seen[candidates[0].Name] = true
}
}
func findCandidates(node *Node, seen map[rune]bool) []*Node {
candidates := []*Node{}
// if we've been seen before, we're clear to recurse
if seen[node.Name] == true {
for _, child := range node.Children { for _, child := range node.Children {
fmt.Printf("%c ", child.Name) candidates = append(candidates, findCandidates(child, seen)...)
} }
fmt.Printf("| Parents: ") return candidates
for _, parent := range node.Parents {
fmt.Printf("%c ", parent.Name)
} }
fmt.Println()
// end debug
// add the node, but only if all its parents have been seen // add the node, but only if all its parents have been seen
if len(node.Parents) == 0 {
seen[node.Name] = true
} else {
for _, parent := range node.Parents { for _, parent := range node.Parents {
if _, ok := seen[parent.Name]; !ok { if seen[parent.Name] == false {
return order return candidates
} }
} }
if _, ok := seen[node.Name]; !ok { // We haven't seen this one before, and its parents are seen.
order = append(order, node) // Return it as a candidate.
fmt.Printf("Added node: %c\n", node.Name) return append(candidates, node)
seen[node.Name] = true
}
}
// recurse
for _, child := range node.Children {
order = append(order, findOrderR(child, seen)...)
}
return order
} }

View File

@ -39,16 +39,15 @@ func BuildDependencyTree(data []string) *Node {
// sort the children in each node // sort the children in each node
for _, node := range depMap { for _, node := range depMap {
sortChildren(node) sortNodes(node.Children)
} }
return BuildTreeRoot(depMap) return BuildTreeRoot(depMap)
} }
func sortChildren(node *Node) { func sortNodes(nodes []*Node) {
children := node.Children sort.Slice(nodes[:], func(i, j int) bool {
sort.Slice(children[:], func(i, j int) bool { return nodes[i].Name < nodes[j].Name
return children[i].Name < children[j].Name
}) })
} }
@ -62,6 +61,6 @@ func BuildTreeRoot(depMap map[rune]*Node) *Node {
} }
} }
sortChildren(root) sortNodes(root.Children)
return root return root
} }