aoc2024/day-14/internal/two/robot.go

52 lines
880 B
Go

package two
import (
"fmt"
)
type position struct {
x int
y int
}
type velocity struct {
x int
y int
}
type robot struct {
position position
velocity velocity
}
func newRobot(px, py, vx, vy int) *robot {
return &robot{
position{x: px, y: py},
velocity{x: vx, y: vy},
}
}
func (r *robot) String() string {
return fmt.Sprintf("position: %+v velocity: %+v", r.position, r.velocity)
}
func (r *robot) update(width, height dimension) position {
oldPosition := r.position
r.position.x += r.velocity.x
if r.position.x < 0 {
r.position.x = int(width) + r.position.x
} else if r.position.x >= int(width) {
r.position.x = r.position.x - int(width)
}
r.position.y += r.velocity.y
if r.position.y < 0 {
r.position.y = int(height) + r.position.y
} else if r.position.y >= int(height) {
r.position.y = r.position.y - int(height)
}
return oldPosition
}