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)
|
||||
|
||||
}
|
93
8/8.go
Normal file
93
8/8.go
Normal file
|
@ -0,0 +1,93 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// cat input | go run 8.go
|
||||
|
||||
func main() {
|
||||
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
registers := make(map[string]int)
|
||||
maxEverVal := 0
|
||||
|
||||
for scanner.Scan() {
|
||||
s := strings.Split(scanner.Text(), " ")
|
||||
|
||||
register := s[0]
|
||||
instruction := s[1]
|
||||
data, _ := strconv.Atoi(s[2])
|
||||
condition := s[3]
|
||||
conditionRegister := s[4]
|
||||
conditionTest := s[5]
|
||||
conditionData, _ := strconv.Atoi(s[6])
|
||||
|
||||
_, present := registers[register]
|
||||
if !present {
|
||||
registers[register] = 0
|
||||
}
|
||||
|
||||
_, present = registers[conditionRegister]
|
||||
if !present {
|
||||
registers[conditionRegister] = 0
|
||||
}
|
||||
|
||||
fmt.Println(s)
|
||||
fmt.Println(registers)
|
||||
|
||||
pass := false
|
||||
if condition == "if" {
|
||||
switch conditionTest {
|
||||
case "==":
|
||||
pass = registers[conditionRegister] == conditionData
|
||||
case ">":
|
||||
pass = registers[conditionRegister] > conditionData
|
||||
case "<":
|
||||
pass = registers[conditionRegister] < conditionData
|
||||
case "<=":
|
||||
pass = registers[conditionRegister] <= conditionData
|
||||
case ">=":
|
||||
pass = registers[conditionRegister] >= conditionData
|
||||
case "!=":
|
||||
pass = registers[conditionRegister] != conditionData
|
||||
default:
|
||||
fmt.Println("Condition failed!", s)
|
||||
}
|
||||
|
||||
if pass {
|
||||
switch instruction {
|
||||
case "inc":
|
||||
registers[register] += data
|
||||
case "dec":
|
||||
registers[register] -= data
|
||||
default:
|
||||
fmt.Println("Invalid instruction", s)
|
||||
}
|
||||
|
||||
if registers[register] > maxEverVal {
|
||||
maxEverVal = registers[register]
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
fmt.Println("Invalid condition", s)
|
||||
}
|
||||
}
|
||||
|
||||
maxVal := 0
|
||||
maxReg := ""
|
||||
for k, v := range registers {
|
||||
if v > maxVal {
|
||||
maxVal = v
|
||||
maxReg = k
|
||||
}
|
||||
}
|
||||
fmt.Println(registers)
|
||||
fmt.Println(maxVal, maxReg, maxEverVal)
|
||||
|
||||
}
|
66
9/9.go
Normal file
66
9/9.go
Normal file
|
@ -0,0 +1,66 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// cat input | go run 9.go
|
||||
|
||||
func main() {
|
||||
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
|
||||
for scanner.Scan() {
|
||||
s := strings.Split(scanner.Text(), "")
|
||||
var keep []string
|
||||
|
||||
// Remove garbage
|
||||
garbage := false
|
||||
garbagecount := 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
|
||||
switch s[i] {
|
||||
case "!":
|
||||
i++
|
||||
continue
|
||||
case "<":
|
||||
if garbage == true {
|
||||
fmt.Println("garbage:", s[i])
|
||||
garbagecount++
|
||||
}
|
||||
garbage = true
|
||||
continue
|
||||
case ">":
|
||||
garbage = false
|
||||
continue
|
||||
default:
|
||||
if !garbage {
|
||||
keep = append(keep, s[i])
|
||||
} else {
|
||||
fmt.Println("garbage:", s[i])
|
||||
garbagecount++
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println(keep)
|
||||
|
||||
// Calculate depth
|
||||
depth := 0
|
||||
total := 0
|
||||
for i := 0; i < len(keep); i++ {
|
||||
switch keep[i] {
|
||||
case "{":
|
||||
depth++
|
||||
total += depth
|
||||
case "}":
|
||||
depth--
|
||||
}
|
||||
}
|
||||
fmt.Println(total, garbagecount)
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue