2019 Day 10 Complete
This commit is contained in:
37
helpers/coordinate.go
Normal file
37
helpers/coordinate.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package aoc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Coordinate struct {
|
||||
X, Y int
|
||||
}
|
||||
|
||||
func CoordinateFromString(str string) *Coordinate {
|
||||
c := Coordinate{}
|
||||
r := strings.NewReader(str)
|
||||
_, err := fmt.Fscanf(r, "[%d, %d]", c.X, c.Y)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return &c
|
||||
}
|
||||
|
||||
func (c Coordinate) Angle(t Coordinate) float64 {
|
||||
ret := math.Atan2(float64(t.X-c.X), float64(c.Y-t.Y)) * 180 / math.Pi
|
||||
if ret < 0 {
|
||||
ret = ret + 360
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (c Coordinate) String() string {
|
||||
return fmt.Sprintf("[%d, %d]", c.X, c.Y)
|
||||
}
|
||||
|
||||
func (c Coordinate) Distance(t Coordinate) int {
|
||||
return AbsInt(c.X-t.X) + AbsInt(c.Y-t.Y)
|
||||
}
|
@@ -27,6 +27,13 @@ const (
|
||||
MIN_INT = -MAX_INT - 1
|
||||
)
|
||||
|
||||
func AbsInt(i int) int {
|
||||
if i < 0 {
|
||||
return i * -1
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
func ArgIsSet(a string) bool {
|
||||
for i := range os.Args {
|
||||
if os.Args[i] == a || strings.HasPrefix(os.Args[i], a+"=") {
|
||||
|
Reference in New Issue
Block a user