refactor: add positions method to grid and use that for iterating in day 4 part 1
This commit is contained in:
@@ -13,6 +13,7 @@ type Grid interface {
|
||||
InBounds(position.Position) bool
|
||||
GetValueAt(position.Position) string
|
||||
GetSameNeighborsOf(position.Position, []move.Move) []position.Position
|
||||
Positions() map[position.Position]string
|
||||
}
|
||||
|
||||
type grid struct {
|
||||
@@ -95,3 +96,14 @@ func (g grid) GetSameNeighborsOf(pos position.Position, moves []move.Move) []pos
|
||||
|
||||
return similarNeighbors
|
||||
}
|
||||
|
||||
func (g grid) Positions() map[position.Position]string {
|
||||
positions := map[position.Position]string{}
|
||||
|
||||
for pos, value := range g.positions {
|
||||
newPosition := position.From(pos.Row(), pos.Column())
|
||||
positions[newPosition] = value
|
||||
}
|
||||
|
||||
return positions
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package grid_test
|
||||
|
||||
import (
|
||||
"maps"
|
||||
"testing"
|
||||
|
||||
"github.com/StevanFreeborn/advent-of-code-2025/internal/grid"
|
||||
@@ -150,3 +151,51 @@ func TestGetSameNeighborsOf(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPositions(t *testing.T) {
|
||||
t.Run("it should return all positions in the grid", func(t *testing.T) {
|
||||
expectedPositions := map[position.Position]string{
|
||||
position.From(0, 0): "@",
|
||||
position.From(0, 1): ".",
|
||||
position.From(0, 2): ".",
|
||||
position.From(1, 0): ".",
|
||||
position.From(1, 1): "@",
|
||||
position.From(1, 2): ".",
|
||||
position.From(2, 0): ".",
|
||||
position.From(2, 1): ".",
|
||||
position.From(2, 2): "@",
|
||||
}
|
||||
|
||||
input := []string{
|
||||
"@..",
|
||||
".@.",
|
||||
"..@",
|
||||
}
|
||||
|
||||
g := grid.From(input)
|
||||
|
||||
positions := g.Positions()
|
||||
|
||||
if maps.Equal(positions, expectedPositions) == false {
|
||||
t.Errorf("expected Positions() to return %v, got %v", expectedPositions, positions)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("it should return a copy of the positions map", func(t *testing.T) {
|
||||
input := []string{
|
||||
"@..",
|
||||
".@.",
|
||||
"..@",
|
||||
}
|
||||
|
||||
g := grid.From(input)
|
||||
|
||||
positions := g.Positions()
|
||||
|
||||
positions[position.From(0, 0)] = "X"
|
||||
|
||||
if g.GetValueAt(position.From(0, 0)) == "X" {
|
||||
t.Errorf("expected modifying the returned positions map to not affect the grid's internal state")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user