feat: add move method to position

This commit is contained in:
Stevan Freeborn
2025-12-13 07:28:13 -06:00
parent 575638e449
commit 934ac56ab6
2 changed files with 19 additions and 0 deletions
+7
View File
@@ -1,9 +1,12 @@
// Package position provides types and functions to represent and manipulate positions and moves on a 2D grid. // Package position provides types and functions to represent and manipulate positions and moves on a 2D grid.
package position package position
import "github.com/StevanFreeborn/advent-of-code-2025/internal/move"
type Position interface { type Position interface {
Row() int Row() int
Column() int Column() int
Move(move.Move) Position
} }
type position struct { type position struct {
@@ -25,3 +28,7 @@ func (p position) Row() int {
func (p position) Column() int { func (p position) Column() int {
return p.column return p.column
} }
func (p position) Move(move move.Move) Position {
return From(p.row+move.NumberOfRows(), p.column+move.NumberOfColumns())
}
+12
View File
@@ -3,6 +3,7 @@ package position_test
import ( import (
"testing" "testing"
"github.com/StevanFreeborn/advent-of-code-2025/internal/move"
"github.com/StevanFreeborn/advent-of-code-2025/internal/position" "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()) 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)
}
}