Day 10 solution
This one was an absolute beating for me. I am so bad at these sorts of problems. Ultimately I settled on a probably-not-ideal solution that crawls the graph with offsets of each variant of (+/-x,+/-y), marking nodes visited as we come across them so that we end up with a list of asteroids that we can see. Given that this is day 10, and knowing how bad I am at math, I'm assuming this is very far from the intended solution, but it works reasonably quickly and I managed to come up with it myself, so I'm not going to stress too much about it. For asteroid destruction, the best method I could come up with for finding the correct order was to implement an entire Vector class and sort by angle, which worked, but again, I can't decide if it was the intended solution or not. I should start reusing past years' codebases so I don't have to keep building a utility library from scratch.
This commit is contained in:
29
utilities/constraints.go
Normal file
29
utilities/constraints.go
Normal file
@ -0,0 +1,29 @@
|
||||
package utilities
|
||||
|
||||
type Ordered interface {
|
||||
Integer | Float | ~string
|
||||
}
|
||||
|
||||
type Signed interface {
|
||||
~int | ~int8 | ~int16 | ~int32 | ~int64
|
||||
}
|
||||
|
||||
type Unsigned interface {
|
||||
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
|
||||
}
|
||||
|
||||
type Integer interface {
|
||||
Signed | Unsigned
|
||||
}
|
||||
|
||||
type Float interface {
|
||||
~float32 | ~float64
|
||||
}
|
||||
|
||||
type Complex interface {
|
||||
~complex64 | ~complex128
|
||||
}
|
||||
|
||||
type Number interface {
|
||||
Integer | Float
|
||||
}
|
17
utilities/map.go
Normal file
17
utilities/map.go
Normal file
@ -0,0 +1,17 @@
|
||||
package utilities
|
||||
|
||||
func MapKeys[T comparable, U any](m map[T]U) []T {
|
||||
r := make([]T, 0, len(m))
|
||||
for k := range m {
|
||||
r = append(r, k)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func MapValues[T comparable, U any](m map[T]U) []U {
|
||||
r := make([]U, 0, len(m))
|
||||
for _, v := range m {
|
||||
r = append(r, v)
|
||||
}
|
||||
return r
|
||||
}
|
39
utilities/vector.go
Normal file
39
utilities/vector.go
Normal file
@ -0,0 +1,39 @@
|
||||
package utilities
|
||||
|
||||
import "math"
|
||||
|
||||
type Vec2[T Number] struct {
|
||||
X T
|
||||
Y T
|
||||
}
|
||||
|
||||
func (v Vec2[T]) Dot(other Vec2[T]) T {
|
||||
return (v.X * other.X) + (v.Y * other.Y)
|
||||
}
|
||||
|
||||
func (v Vec2[T]) Len() T {
|
||||
return T(math.Sqrt(float64(v.LenSquared())))
|
||||
}
|
||||
|
||||
func (v Vec2[T]) LenSquared() T {
|
||||
return (v.X * v.X) + (v.Y * v.Y)
|
||||
}
|
||||
|
||||
func (v Vec2[T]) To(other Vec2[T]) Vec2[T] {
|
||||
return Vec2[T]{
|
||||
X: v.X - other.X,
|
||||
Y: v.Y - other.Y,
|
||||
}
|
||||
}
|
||||
|
||||
func (v Vec2[T]) AngleBetween(other Vec2[T]) float64 {
|
||||
rad := math.Atan2(float64(other.Y-v.Y), float64(other.X-v.X))
|
||||
return rad * 180 / math.Pi
|
||||
}
|
||||
|
||||
func VecBetween[T Number](a, b Vec2[T]) Vec2[T] {
|
||||
return Vec2[T]{
|
||||
X: a.X - b.X,
|
||||
Y: a.Y - b.Y,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user