-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
173 lines (140 loc) · 3.36 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strings"
)
func main() {
// Use Flags to run a part
methodP := flag.String("method", "p1", "The method/part that should be run, valid are p1,p2 and test")
flag.Parse()
switch *methodP {
case "p1":
PartOne()
break
case "p2":
PartTwo()
break
case "test":
break
}
}
func PartOne() {
input := readInput()
root := BuildTree(input)
fmt.Println("The number of orbits is", Walk(&root))
}
func Contains(a []string, x string) bool {
for _, n := range a {
if x == n {
return true
}
}
return false
}
// Walk traverses a tree depth-first,
func Walk(t *Tree) int {
if t.children == nil {
return t.orbits
} else {
total := t.orbits
for _, child := range t.children {
total += Walk(child)
}
return total
}
}
func PartTwo() {
input := readInput()
root := BuildTree(input)
// Find shortest
lowestSanta := 10000
lowestYou := 10000
currentNode := root
for {
currentNodeName := currentNode.name
for _, child := range currentNode.children {
distanceToSanta, found := FindDistance(child, "SAN")
distanceToYou, found2 := FindDistance(child, "YOU")
if found && found2 && distanceToSanta < lowestSanta && distanceToYou < lowestYou {
lowestSanta = distanceToSanta
lowestYou = distanceToYou
currentNode = *child
break
}
}
if currentNodeName == currentNode.name {
break
}
}
s, _ := FindDistance(¤tNode, "SAN")
y, _ := FindDistance(¤tNode, "YOU")
fmt.Println("Distance to Santa is", s+y)
}
// FindDistance looks for a node, and returns the distance to reach it
func FindDistance(t *Tree, nodeName string) (int, bool) {
if t.name == nodeName {
return -1, true
} else if t.children == nil {
return 0, false
} else {
total := 1
f := false
for _, child := range t.children {
t, found := FindDistance(child, nodeName)
if found {
total += t
f = true
}
}
return total, f
}
}
func BuildTree(input []string) Tree {
var orbitTarget []string
var orbitObject []string
for _, orbit := range input {
// Split string again
orbitItems := strings.Split(orbit, ")")
// First find root item
orbitTarget = append(orbitTarget, orbitItems[0])
orbitObject = append(orbitObject, orbitItems[1])
}
// Root node is COM
nodesToCheck := []*Tree{}
root := Tree{name: "COM", orbits: 0}
nodesToCheck = append(nodesToCheck, &root)
for len(nodesToCheck) > 0 {
for i, orbitT := range orbitTarget {
if orbitT == nodesToCheck[0].name {
newTree := Tree{name: orbitObject[i], orbits: nodesToCheck[0].orbits + 1}
nodesToCheck = append(nodesToCheck, &newTree)
nodesToCheck[0].children = append(nodesToCheck[0].children, &newTree)
}
}
// now remove first node from nodesToCheck
// Remove the element at index i from a.
copy(nodesToCheck[0:], nodesToCheck[1:]) // Shift a[i+1:] left one index.
nodesToCheck[len(nodesToCheck)-1] = &Tree{} // Erase last element (write zero value).
nodesToCheck = nodesToCheck[:len(nodesToCheck)-1] // Truncate slice.
}
return root
}
type Tree struct {
name string
orbits int
children []*Tree
}
// Read data from input.txt
// Return the string, so that we can deal with it however
func readInput() []string {
var input []string
f, _ := os.Open("input.txt")
scanner := bufio.NewScanner(f)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
}