From bdba3a22681096a94c7dab9b7b554046d1251e78 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 28 Dec 2023 10:46:00 -0600 Subject: [PATCH] 2023 Day 24 Complete! --- 2023/day24/main.go | 51 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/2023/day24/main.go b/2023/day24/main.go index 7a81ffd..939861d 100644 --- a/2023/day24/main.go +++ b/2023/day24/main.go @@ -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++ {