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.
for _, child := range node.Children { candidates := findCandidates(root, seen)
fmt.Printf("%c ", child.Name)
if len(candidates) == 0 {
return order
}
sortNodes(candidates)
order = append(order, candidates[0])
seen[candidates[0].Name] = true
} }
fmt.Printf("| Parents: ") }
for _, parent := range node.Parents {
fmt.Printf("%c ", parent.Name) 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 {
candidates = append(candidates, findCandidates(child, seen)...)
}
return candidates
} }
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 { for _, parent := range node.Parents {
seen[node.Name] = true if seen[parent.Name] == false {
} else { return candidates
for _, parent := range node.Parents {
if _, ok := seen[parent.Name]; !ok {
return order
}
}
if _, ok := seen[node.Name]; !ok {
order = append(order, node)
fmt.Printf("Added node: %c\n", node.Name)
seen[node.Name] = true
} }
} }
// recurse // We haven't seen this one before, and its parents are seen.
for _, child := range node.Children { // Return it as a candidate.
order = append(order, findOrderR(child, seen)...) return append(candidates, node)
}
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
} }