feat: add move method to position
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
// Package position provides types and functions to represent and manipulate positions and moves on a 2D grid.
|
||||
package position
|
||||
|
||||
import "github.com/StevanFreeborn/advent-of-code-2025/internal/move"
|
||||
|
||||
type Position interface {
|
||||
Row() int
|
||||
Column() int
|
||||
Move(move.Move) Position
|
||||
}
|
||||
|
||||
type position struct {
|
||||
@@ -25,3 +28,7 @@ func (p position) Row() int {
|
||||
func (p position) Column() int {
|
||||
return p.column
|
||||
}
|
||||
|
||||
func (p position) Move(move move.Move) Position {
|
||||
return From(p.row+move.NumberOfRows(), p.column+move.NumberOfColumns())
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package position_test
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/StevanFreeborn/advent-of-code-2025/internal/move"
|
||||
"github.com/StevanFreeborn/advent-of-code-2025/internal/position"
|
||||
)
|
||||
|
||||
@@ -20,3 +21,14 @@ func TestFrom(t *testing.T) {
|
||||
t.Errorf("expected column to be %d, got %d", column, pos.Column())
|
||||
}
|
||||
}
|
||||
|
||||
func TestMove(t *testing.T) {
|
||||
expectedPos := position.From(3, 4)
|
||||
startPos := position.From(2, 4)
|
||||
|
||||
result := startPos.Move(move.Down)
|
||||
|
||||
if result != expectedPos {
|
||||
t.Errorf("expected position to be %+v, got %+v", expectedPos, result)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user