Leaving Code & Coffee
This commit is contained in:
parent
4cfa9412b3
commit
2e01de882d
@ -14,9 +14,89 @@ func main() {
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
5
2023/day24/testinput
Normal file
5
2023/day24/testinput
Normal file
@ -0,0 +1,5 @@
|
||||
19, 13, 30 @ -2, 1, -2
|
||||
18, 19, 22 @ -1, -1, -2
|
||||
20, 25, 34 @ -2, -2, -4
|
||||
12, 31, 28 @ -1, -2, -1
|
||||
20, 19, 15 @ 1, -5, -3
|
Loading…
Reference in New Issue
Block a user