2023 Day 24 Complete!

This commit is contained in:
Brian Buller 2023-12-28 10:46:00 -06:00
parent 2e01de882d
commit bdba3a2268
1 changed files with 50 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
"golang.org/x/exp/slices"
)
func main() {
@ -43,7 +44,6 @@ func part2(inp []string) {
fmt.Println(findThrow(all))
}
// https://github.com/rumkugel13/AdventOfCode2023/blob/main/day24.go#L139
func findThrow(all []*hailstone) int {
maybeX, maybeY, maybeZ := []int{}, []int{}, []int{}
for i := 0; i < len(all)-1; i++ {
@ -51,10 +51,59 @@ func findThrow(all []*hailstone) int {
a, b := all[i], all[j]
if a.DX == b.DX {
nextMaybe := findMatchingVelocity(int(b.X-a.X), int(a.DX))
if len(maybeX) == 0 {
maybeX = nextMaybe
} else {
maybeX = getIntersect(maybeX, nextMaybe)
}
}
if a.DY == b.DY {
nextMaybe := findMatchingVelocity(int(b.Y-a.Y), int(a.DY))
if len(maybeY) == 0 {
maybeY = nextMaybe
} else {
maybeY = getIntersect(maybeY, nextMaybe)
}
}
if a.DZ == b.DZ {
nextMaybe := findMatchingVelocity(int(b.Z-a.Z), int(a.DZ))
if len(maybeZ) == 0 {
maybeZ = nextMaybe
} else {
maybeZ = getIntersect(maybeZ, nextMaybe)
}
}
}
}
if len(maybeX) == len(maybeY) && len(maybeY) == len(maybeZ) && len(maybeZ) == 1 {
rockVel := hailstone{
X: float64(maybeX[0]), Y: float64(maybeY[0]), Z: float64(maybeZ[0]),
}
hailA, hailB := all[0], all[1]
mA := (hailA.DY - rockVel.Y) / (hailA.DX - rockVel.X)
mB := (hailB.DY - rockVel.Y) / (hailB.DX - rockVel.X)
cA := hailA.Y - (mA * hailA.X)
cB := hailB.Y - (mB * hailB.X)
xPos := (cB - cA) / (mA - mB)
yPos := mA*xPos + cA
time := (xPos - hailA.X) / (hailA.DX - rockVel.X)
zPos := hailA.Z + (hailA.DZ-rockVel.Z)*time
return int(xPos + yPos + zPos)
}
return h.MIN_INT
}
func getIntersect(a, b []int) []int {
result := []int{}
for _, val := range a {
if slices.Contains(b, val) {
result = append(result, val)
}
}
return result
}
func findMatchingVelocity(diff, vel int) []int {
match := []int{}
for v := -1000; v < 1000; v++ {