advent-of-code/hacker01/hacker.go

70 lines
1.2 KiB
Go
Raw Normal View History

2024-04-05 16:27:02 -06:00
package main
import "fmt"
import "os"
import "bufio"
import "strconv"
import "strings"
var input1 = strings.NewReader(`
6
1 2 5 3 6 4
`)
var input2 = strings.NewReader(`
15
1 14 3 7 4 5 15 6 13 10 11 2 12 8 9
`)
var input3 = os.Stdin
func main() {
var length int
var tree *node
scanner := bufio.NewScanner(input1)
for scanner.Scan() {
if length == 0 {
length, _ = strconv.Atoi(scanner.Text())
continue
}
for _, txt := range strings.Fields(scanner.Text()) {
if v, err := strconv.Atoi(txt); err == nil {
tree = insert(tree, &node{value: v})
}
}
}
foo(input{1, "hi"})
fmt.Println(tree)
}
type node struct {
value int
left *node
right *node
}
func insert(root, n *node) *node {
if root == nil {
return n
}
if root.value > n.value {
root.left = insert(root.left, n)
return root
}
root.right = insert(root.right, n)
return root
}
func (n *node) String() string {
if n == nil { return "" }
return fmt.Sprintf("%v %v%v", n.value, n.left.String(), n.right.String())
}
type input struct{a int; b string}
func foo (in input) {}