mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-07 20:53:30 +02:00
might be making a mistake starting 2018...
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"adventofcode/util"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
input := util.ReadFile("../input.txt")
|
||||
|
||||
// split it into a slice of all strings
|
||||
inputSliced := strings.Split(input, ",")
|
||||
|
||||
// send the input to a helper function that will return a map {planetName string: planetItOrbits string}
|
||||
mapOfOrbits := makeGraphDependencyList(inputSliced)
|
||||
|
||||
result := 0
|
||||
|
||||
// don't care about outer planet value
|
||||
for _, innerPlanet := range mapOfOrbits {
|
||||
result += recurseToCOM(mapOfOrbits, innerPlanet)
|
||||
}
|
||||
fmt.Println(result)
|
||||
}
|
||||
|
||||
func makeGraphDependencyList(inputSlice []string) map[string]string {
|
||||
resultMap := make(map[string]string)
|
||||
for _, v := range inputSlice {
|
||||
slicedPlanets := strings.Split(v, ")")
|
||||
inner, outer := slicedPlanets[0], slicedPlanets[1]
|
||||
// set the key-value pair on the map/"object"
|
||||
resultMap[outer] = inner
|
||||
}
|
||||
|
||||
return resultMap
|
||||
}
|
||||
|
||||
func recurseToCOM(mapOfOrbits map[string]string, planet string) int {
|
||||
// start the result at one because triggering this function is using the outerPlanet
|
||||
// and needs to include that initial orbit from innerPlanet to outerPlanet
|
||||
result := 1
|
||||
for {
|
||||
planetString, ok := mapOfOrbits[planet]
|
||||
if ok == true {
|
||||
result++
|
||||
planet = planetString
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user