aoc2024/day-06/internal/one/point.go

52 lines
854 B
Go
Raw Normal View History

2024-12-06 20:01:17 +00:00
package one
import "fmt"
type coords struct {
X int
Y int
}
type point struct {
coords
direction int
}
func (p *point) String() string {
return fmt.Sprintf("X: %d Y:%d Direction: %s", p.X, p.Y, []string{"N", "E", "S", "W"}[p.direction])
}
func (p *point) recalibrate() {
switch p.direction {
case N:
p.Y++
case E:
p.X--
case S:
p.Y--
case W:
p.X++
}
if p.direction == W {
p.direction = 0
} else {
p.direction++
}
}
func nextPoint(current point) point {
switch current.direction {
case N:
return point{coords{current.X, current.Y - 1}, current.direction}
case E:
return point{coords{current.X + 1, current.Y}, current.direction}
case S:
return point{coords{current.X, current.Y + 1}, current.direction}
case W:
return point{coords{current.X - 1, current.Y}, current.direction}
default:
return point{}
}
}