mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 06:40:47 +00:00
52 lines
880 B
Go
52 lines
880 B
Go
package one
|
|
|
|
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
|
|
}
|