103 lines
2.2 KiB
Go
103 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
h "git.bullercodeworks.com/brian/adventofcode/helpers"
|
|
)
|
|
|
|
func main() {
|
|
inp := h.StdinToStringSlice()
|
|
part1(inp)
|
|
fmt.Println()
|
|
part2(inp)
|
|
}
|
|
|
|
func part1(inp []string) {
|
|
//testMin, testMax := float64(7), float64(27) // Test Input
|
|
testMin, testMax := float64(200000000000000), float64(400000000000000)
|
|
var all []*hailstone
|
|
for _, i := range inp {
|
|
all = append(all, NewHailstone(i))
|
|
}
|
|
var count int
|
|
for i := 0; i < len(all)-1; i++ {
|
|
for j := i + 1; j < len(all); j++ {
|
|
x, y := findIntersection(all[i], all[j])
|
|
if x >= testMin && x <= testMax && y >= testMin && y <= testMax {
|
|
count++
|
|
}
|
|
}
|
|
}
|
|
|
|
fmt.Println("# Part 1")
|
|
fmt.Println(count)
|
|
}
|
|
|
|
func part2(inp []string) {
|
|
var all []*hailstone
|
|
for _, i := range inp {
|
|
all = append(all, NewHailstone(i))
|
|
}
|
|
fmt.Println("# Part 2")
|
|
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++ {
|
|
for j := i + 1; j < len(all); j++ {
|
|
a, b := all[i], all[j]
|
|
if a.DX == b.DX {
|
|
nextMaybe := findMatchingVelocity(int(b.X-a.X), int(a.DX))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
func findMatchingVelocity(diff, vel int) []int {
|
|
match := []int{}
|
|
for v := -1000; v < 1000; v++ {
|
|
if v != vel && diff%(v-vel) == 0 {
|
|
match = append(match, v)
|
|
}
|
|
}
|
|
return match
|
|
}
|
|
|
|
func slope(h *hailstone) float64 {
|
|
return h.DY / h.DX
|
|
}
|
|
|
|
func findIntersection(i, j *hailstone) (float64, float64) {
|
|
if slope(i) == slope(j) {
|
|
return -1, -1
|
|
}
|
|
cx := (j.Y - (j.DY/j.DX)*j.X - (i.Y - (i.DY/i.DX)*i.X)) / (i.DY/i.DX - j.DY/j.DX)
|
|
perp := !((cx > i.X) == (i.DX > 0) && (cx > j.X) == (j.DX > 0))
|
|
if perp {
|
|
return -1, -1
|
|
}
|
|
|
|
y := ((-i.DY/j.DY)*(j.X*j.DY-j.Y*j.DX) + i.X*i.DY - i.Y*i.DX) / ((j.DX * i.DY / j.DY) - i.DX)
|
|
x := y*j.DX/j.DY + (j.X*j.DY-j.Y*j.DX)/j.DY
|
|
return x, y
|
|
}
|
|
|
|
type hailstone struct {
|
|
X, Y, Z float64
|
|
DX, DY, DZ float64
|
|
}
|
|
|
|
func NewHailstone(inp string) *hailstone {
|
|
h := hailstone{}
|
|
fmt.Sscanf(inp, "%f, %f, %f @ %f, %f, %f",
|
|
&h.X, &h.Y, &h.Z, &h.DX, &h.DY, &h.DZ)
|
|
return &h
|
|
}
|
|
|
|
func (h hailstone) String() string {
|
|
return fmt.Sprintf("%f, %f, %f @ %f, %f, %f",
|
|
h.X, h.Y, h.Z, h.DX, h.DY, h.DZ)
|
|
}
|