days 7, 8 and 9
This commit is contained in:
parent
c3bc233d82
commit
48552fc07e
4 changed files with 328 additions and 0 deletions
86
7/7.2.go
Normal file
86
7/7.2.go
Normal file
|
@ -0,0 +1,86 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// cat input | go run 7.2.go
|
||||
|
||||
type node struct {
|
||||
NodeName string
|
||||
NodeWeight int
|
||||
Children []string
|
||||
ChildWeight int
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
allNodes := make(map[string]node)
|
||||
|
||||
for scanner.Scan() {
|
||||
s := strings.Split(scanner.Text(), " -> ")
|
||||
var children []string
|
||||
|
||||
if len(s) == 2 {
|
||||
// Node has children
|
||||
children = strings.Split(s[1], ", ")
|
||||
}
|
||||
|
||||
// Name and Weight
|
||||
tNode := strings.Split(s[0], " ")
|
||||
node := node{NodeName: tNode[0]}
|
||||
node.NodeWeight, _ = strconv.Atoi(strings.TrimRight(strings.TrimLeft(tNode[1], "("), ")"))
|
||||
node.Children = children
|
||||
allNodes[node.NodeName] = node
|
||||
}
|
||||
|
||||
for _, n := range allNodes {
|
||||
n.ChildWeight = weighChildren(allNodes, n.NodeName)
|
||||
|
||||
}
|
||||
|
||||
fmt.Println(allNodes)
|
||||
|
||||
}
|
||||
|
||||
func weighChildren(allNodes map[string]node, child string) int {
|
||||
|
||||
if len(allNodes[child].Children) == 0 {
|
||||
return allNodes[child].NodeWeight
|
||||
}
|
||||
|
||||
sum := allNodes[child].NodeWeight
|
||||
check := 0
|
||||
uneven := false
|
||||
|
||||
for _, e := range allNodes[child].Children {
|
||||
|
||||
r := weighChildren(allNodes, e)
|
||||
sum += r
|
||||
|
||||
if check == 0 {
|
||||
check = r
|
||||
}
|
||||
|
||||
if check != r {
|
||||
uneven = true
|
||||
}
|
||||
}
|
||||
|
||||
f := allNodes[child]
|
||||
f.ChildWeight = sum
|
||||
allNodes[child] = f
|
||||
|
||||
if uneven {
|
||||
for _, e := range allNodes[child].Children {
|
||||
fmt.Println(e, allNodes[e].ChildWeight, allNodes[e].NodeWeight)
|
||||
}
|
||||
os.Exit(0)
|
||||
}
|
||||
return sum
|
||||
}
|
83
7/7.go
Normal file
83
7/7.go
Normal file
|
@ -0,0 +1,83 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// cat input | go run 7.go
|
||||
|
||||
type node struct {
|
||||
NodeName string
|
||||
NodeWeight int
|
||||
Children []string
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
allNodes := make(map[string]node)
|
||||
|
||||
for scanner.Scan() {
|
||||
|
||||
s := strings.Split(scanner.Text(), " -> ")
|
||||
var children []string
|
||||
|
||||
if len(s) == 2 {
|
||||
// Node has children
|
||||
children = strings.Split(s[1], ", ")
|
||||
}
|
||||
|
||||
// Name and Weight
|
||||
tNode := strings.Split(s[0], " ")
|
||||
node := node{NodeName: tNode[0]}
|
||||
node.NodeWeight, _ = strconv.Atoi(strings.TrimRight(strings.TrimLeft(tNode[1], "("), ")"))
|
||||
node.Children = children
|
||||
|
||||
fmt.Println(node)
|
||||
allNodes[node.NodeName] = node
|
||||
|
||||
}
|
||||
|
||||
fmt.Println(allNodes)
|
||||
|
||||
// I expect this is going to bite us in part 2, but let's just repeatedly iterate
|
||||
// over the map until all the children are removed. I think this should be quicker
|
||||
// than traversing the tree repeatedly, given we don't know the root node.
|
||||
removed := make(map[string]bool)
|
||||
|
||||
for len(allNodes) > 0 {
|
||||
for k, n := range allNodes {
|
||||
fmt.Println("node:", k)
|
||||
if len(n.Children) == 0 {
|
||||
fmt.Println("removing node", k)
|
||||
removed[k] = true
|
||||
delete(allNodes, k)
|
||||
|
||||
} else {
|
||||
|
||||
if len(allNodes[k].Children) > 0 {
|
||||
c := n.Children[0]
|
||||
if removed[c] == true {
|
||||
fmt.Println("len:", len(allNodes[k].Children))
|
||||
var new []string
|
||||
fmt.Println("nchildren:", n.Children)
|
||||
for j := 0; j < len(n.Children)-1; j++ {
|
||||
fmt.Println("j:", j)
|
||||
new = append(new, n.Children[j+1])
|
||||
}
|
||||
n.Children = new
|
||||
allNodes[k] = n
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println(allNodes)
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue