refactor: extract grid abstraction for reuse
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
// Package position provides types and functions to represent and manipulate positions and moves on a 2D grid.
|
||||
package position
|
||||
|
||||
type Position interface {
|
||||
Row() int
|
||||
Column() int
|
||||
}
|
||||
|
||||
type position struct {
|
||||
row int
|
||||
column int
|
||||
}
|
||||
|
||||
func From(row int, column int) Position {
|
||||
return position{
|
||||
row: row,
|
||||
column: column,
|
||||
}
|
||||
}
|
||||
|
||||
func (p position) Row() int {
|
||||
return p.row
|
||||
}
|
||||
|
||||
func (p position) Column() int {
|
||||
return p.column
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package position_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/StevanFreeborn/advent-of-code-2025/internal/position"
|
||||
)
|
||||
|
||||
func TestFrom(t *testing.T) {
|
||||
row := 3
|
||||
column := 5
|
||||
|
||||
pos := position.From(row, column)
|
||||
|
||||
if pos.Row() != row {
|
||||
t.Errorf("expected row to be %d, got %d", row, pos.Row())
|
||||
}
|
||||
|
||||
if pos.Column() != column {
|
||||
t.Errorf("expected column to be %d, got %d", column, pos.Column())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user