commit b5147142257d6d78aa1f32060597f166f6f15bf7 Author: Preston Baxter Date: Mon Aug 19 23:44:24 2024 -0700 I did too much diff --git a/cmd/18/main.go b/cmd/18/main.go new file mode 100644 index 0000000..8291f3c --- /dev/null +++ b/cmd/18/main.go @@ -0,0 +1,184 @@ +package main + +import ( + "bufio" + // "container/heap" + "fmt" + "strconv" + "strings" + + ds "euler/pkg/datastructures" +) + +/* +75 +95 64 +17 47 82 +18 35 87 10 +20 04 82 47 65 +19 01 23 75 03 34 +88 02 77 73 07 63 67 +99 65 04 28 06 16 70 92 +41 41 26 56 83 40 80 70 33 +41 48 72 33 47 32 37 16 94 29 +53 71 44 65 25 43 91 52 97 51 14 +70 11 33 28 77 73 17 78 39 68 17 57 +91 71 52 38 17 14 91 43 58 50 27 29 48 +63 66 04 68 89 53 67 30 73 16 69 87 40 31 +04 62 98 27 23 09 70 98 73 93 38 53 60 04 23 + + + +*/ + +var ( + smallTreeStr = `3 +7 4 +2 4 6 +8 5 9 3` + treeStr = `75 +95 64 +17 47 82 +18 35 87 10 +20 04 82 47 65 +19 01 23 75 03 34 +88 02 77 73 07 63 67 +99 65 04 28 06 16 70 92 +41 41 26 56 83 40 80 70 33 +41 48 72 33 47 32 37 16 94 29 +53 71 44 65 25 43 91 52 97 51 14 +70 11 33 28 77 73 17 78 39 68 17 57 +91 71 52 38 17 14 91 43 58 50 27 29 48 +63 66 04 68 89 53 67 30 73 16 69 87 40 31 +04 62 98 27 23 09 70 98 73 93 38 53 60 04 23` + + invertTreeStr = `25 +5 36 +83 53 18 +82 65 13 90 +80 96 18 53 35 +81 99 77 25 97 66 +12 98 23 27 93 37 33 +1 35 96 72 94 84 30 8 +59 59 74 44 17 60 20 30 67 +59 52 28 67 53 68 63 84 6 71 +47 29 56 35 75 57 9 48 3 49 86 +30 89 67 72 23 27 83 22 61 32 83 43 +9 29 48 62 83 86 9 57 42 50 73 71 52 +37 34 96 32 11 47 33 70 27 84 31 13 60 69 +96 38 2 73 77 91 30 2 27 7 62 47 40 96 77` + + inTree Tree = TreeFromString(invertTreeStr) + valTree Tree = TreeFromString(treeStr) + wTree Tree = BlankTree(15) + lvTree Tree = BlankTree(15) + + shortSet = map[string]int{} + shortHeap = make(ds.MinHeap, 0, 10) +) + +type Tree [][]int +type Node struct { + Level int + Row int +} + +func TreeFromString(str string) Tree { + reader := bufio.NewScanner(strings.NewReader(str)) + t := make(Tree, 0, 0) + for reader.Scan() { + split := strings.Split(reader.Text(), " ") + v := []int{} + for _, s := range split { + i, _ := strconv.ParseInt(s, 10, 32) + v = append(v, int(i)) + } + t = append(t, v) + } + + return t +} + +func BlankTree(depth int) Tree { + t := make(Tree, 1) + t[0] = []int{0} + for i := 1; i < depth; i += 1 { + v := make([]int, i+1) + t = append(t, v) + } + + return t +} + +func (n Node) key() string { + return fmt.Sprintf("%d:%d", n.Level, n.Row) +} + +func (t Tree) root() int { + if len(t) > 0 && len(t[0]) == 1 { + return t[0][0] + } + return -1 +} + +func (t Tree) setValue(n Node, val int) { + if n.Level < len(t) && n.Row < len(t[n.Level]) { + t[n.Level][n.Row] = val + } +} + +func (t Tree) value(n Node) int { + if n.Level < len(t) && n.Row < len(t[n.Level]) { + return t[n.Level][n.Row] + } + return -1 +} + +func (t Tree) left(n Node) (int, Node) { + if n.Level+1 < len(t) && n.Row < len(t[n.Level+1]) { + return t[n.Level+1][n.Row], Node{n.Level + 1, n.Row} + } + return -1, Node{-1, -1} +} + +func (t Tree) right(n Node) (int, Node) { + if n.Level+1 < len(t) && n.Row+1 < len(t[n.Level+1]) { + return t[n.Level+1][n.Row+1], Node{n.Level + 1, n.Row + 1} + } + return -1, Node{-1, -1} + +} + +func (t Tree) String() string { + s := "" + for _, row := range t { + for i, n := range row { + s += fmt.Sprintf("%d", n) + if i < len(row)-1 { + s += " " + } + } + s += "\n" + } + + return s +} + +func NodeFromKey(k string) Node { + split := strings.Split(k, ":") + a, _ := strconv.ParseInt(split[0], 10, 32) + b, _ := strconv.ParseInt(split[1], 10, 32) + + return Node{int(a), int(b)} +} + +func main() { + + for i := len(valTree) -2; i >= 0; i-=1 { + for j := 0; j <= i; j+=1 { + valTree[i][j] += max(valTree[i+1][j], valTree[i+1][j+1]) + } + } + + fmt.Printf("%d\n", valTree[0][0]) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a82ed97 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module euler + +go 1.21.5 diff --git a/pkg/datastructures/heap.go b/pkg/datastructures/heap.go new file mode 100644 index 0000000..d117014 --- /dev/null +++ b/pkg/datastructures/heap.go @@ -0,0 +1,38 @@ +package datastructures + +import "container/heap" + +type HeapItem struct { + Value any + Priority int + index int +} + +type MinHeap []*HeapItem + +func (m MinHeap) Len() int { return len(m) } +func (m MinHeap) Less(i, j int) bool { return m[i].Priority < m[j].Priority } +func (m MinHeap) Swap(i, j int) { + m[i], m[j] = m[j], m[i] + m[i].index = i + m[j].index = j +} + +func (m *MinHeap) Push(x any) { + *m = append(*m, x.(*HeapItem)) +} + +func (m *MinHeap) Pop() any { + old := *m + n := len(old) + item := old[n-1] + item.index = 0 + old[n-1] = nil + *m = old[0 : n-1] + return item +} + +func (m *MinHeap) Update(item *HeapItem, priority int) { + item.Priority = priority + heap.Fix(m, item.index) +}