mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-07 20:53:30 +02:00
cleanup part 1
This commit is contained in:
+47
-43
@@ -20,53 +20,41 @@ func main() {
|
||||
// fmt.Println(stringSlice)
|
||||
|
||||
// need to set x y z, dx, dy, dz of each of the starting moons
|
||||
sliceMoons := make([]moon, 0)
|
||||
for _, str := range stringSlice {
|
||||
x := str[strings.Index(str, "x=")+2 : strings.Index(str, ",")]
|
||||
// this is gross
|
||||
y := str[strings.Index(str, "y=")+2 : strings.Index(str, "y=")+strings.Index(str[strings.Index(str, ",")+1:], ",")-1]
|
||||
z := str[strings.Index(str, "z=")+2 : len(str)-1]
|
||||
|
||||
intx, _ := strconv.Atoi(x)
|
||||
inty, _ := strconv.Atoi(y)
|
||||
intz, _ := strconv.Atoi(z)
|
||||
sliceMoons = append(sliceMoons, moon{intx, inty, intz, 0, 0, 0})
|
||||
}
|
||||
sliceMoons := makeMoonSlice(stringSlice)
|
||||
// fmt.Println(sliceMoons)
|
||||
|
||||
// iterate through for each step
|
||||
for i := 0; i < 1000; i++ {
|
||||
// iterate through all the moons in the slice
|
||||
for index, element := range sliceMoons {
|
||||
for start := 0; start < len(sliceMoons); start++ {
|
||||
// update velocities dx dy dz
|
||||
// requires iterating through all of the moons again...
|
||||
for restIndex := index + 1; restIndex < len(sliceMoons); restIndex++ {
|
||||
if element.x < sliceMoons[restIndex].x {
|
||||
sliceMoons[index].dx++
|
||||
for restIndex := start + 1; restIndex < len(sliceMoons); restIndex++ {
|
||||
if sliceMoons[start].x < sliceMoons[restIndex].x {
|
||||
sliceMoons[start].dx++
|
||||
sliceMoons[restIndex].dx--
|
||||
} else if element.x > sliceMoons[restIndex].x {
|
||||
sliceMoons[index].dx--
|
||||
} else if sliceMoons[start].x > sliceMoons[restIndex].x {
|
||||
sliceMoons[start].dx--
|
||||
sliceMoons[restIndex].dx++
|
||||
}
|
||||
if element.y < sliceMoons[restIndex].y {
|
||||
sliceMoons[index].dy++
|
||||
if sliceMoons[start].y < sliceMoons[restIndex].y {
|
||||
sliceMoons[start].dy++
|
||||
sliceMoons[restIndex].dy--
|
||||
} else if element.y > sliceMoons[restIndex].y {
|
||||
sliceMoons[index].dy--
|
||||
} else if sliceMoons[start].y > sliceMoons[restIndex].y {
|
||||
sliceMoons[start].dy--
|
||||
sliceMoons[restIndex].dy++
|
||||
}
|
||||
if element.z < sliceMoons[restIndex].z {
|
||||
sliceMoons[index].dz++
|
||||
if sliceMoons[start].z < sliceMoons[restIndex].z {
|
||||
sliceMoons[start].dz++
|
||||
sliceMoons[restIndex].dz--
|
||||
} else if element.z > sliceMoons[restIndex].z {
|
||||
sliceMoons[index].dz--
|
||||
} else if sliceMoons[start].z > sliceMoons[restIndex].z {
|
||||
sliceMoons[start].dz--
|
||||
sliceMoons[restIndex].dz++
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// then update coordinates x y z
|
||||
}
|
||||
|
||||
// then update coordinates x y z
|
||||
for i2, e := range sliceMoons {
|
||||
sliceMoons[i2].x += e.dx
|
||||
sliceMoons[i2].y += e.dy
|
||||
@@ -79,20 +67,6 @@ func main() {
|
||||
fmt.Println(kinetic(sliceMoons))
|
||||
}
|
||||
|
||||
func kinetic(moons []moon) (result int) {
|
||||
for _, e := range moons {
|
||||
sumXYZ := abs(e.x) + abs(e.y) + abs(e.z)
|
||||
velXYZ := abs(e.dx) + abs(e.dy) + abs(e.dz)
|
||||
result += (sumXYZ * velXYZ)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func abs(value int) int {
|
||||
flt := float64(value)
|
||||
return int(math.Abs(flt))
|
||||
}
|
||||
|
||||
func readInputFile(path string) []string {
|
||||
// var pixelString string
|
||||
pixelSlice := make([]string, 0)
|
||||
@@ -114,3 +88,33 @@ func readInputFile(path string) []string {
|
||||
// return pixelString
|
||||
return pixelSlice
|
||||
}
|
||||
|
||||
func makeMoonSlice(stringSlice []string) []moon {
|
||||
sliceMoons := make([]moon, 0)
|
||||
for _, str := range stringSlice {
|
||||
x := str[strings.Index(str, "x=")+2 : strings.Index(str, ",")]
|
||||
// this is gross
|
||||
y := str[strings.Index(str, "y=")+2 : strings.Index(str, "y=")+strings.Index(str[strings.Index(str, ",")+1:], ",")-1]
|
||||
z := str[strings.Index(str, "z=")+2 : len(str)-1]
|
||||
|
||||
intx, _ := strconv.Atoi(x)
|
||||
inty, _ := strconv.Atoi(y)
|
||||
intz, _ := strconv.Atoi(z)
|
||||
sliceMoons = append(sliceMoons, moon{intx, inty, intz, 0, 0, 0})
|
||||
}
|
||||
return sliceMoons
|
||||
}
|
||||
|
||||
func kinetic(moons []moon) (result int) {
|
||||
for _, e := range moons {
|
||||
sumXYZ := abs(e.x) + abs(e.y) + abs(e.z)
|
||||
velXYZ := abs(e.dx) + abs(e.dy) + abs(e.dz)
|
||||
result += (sumXYZ * velXYZ)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func abs(value int) int {
|
||||
flt := float64(value)
|
||||
return int(math.Abs(flt))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user