From 5586641afffc23d6f788f9b56cf6ec6267d2bf83 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Sun, 7 Dec 2025 21:33:11 -0600 Subject: [PATCH] refactor: add positions method to grid and use that for iterating in day 4 part 1 --- cmd/04/main.go | 27 +++++++-------------- internal/grid/grid.go | 12 ++++++++++ internal/grid/grid_test.go | 49 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 19 deletions(-) diff --git a/cmd/04/main.go b/cmd/04/main.go index 1395edf..c2803d6 100644 --- a/cmd/04/main.go +++ b/cmd/04/main.go @@ -1,12 +1,9 @@ package main import ( - "fmt" - "github.com/StevanFreeborn/advent-of-code-2025/internal/file" "github.com/StevanFreeborn/advent-of-code-2025/internal/grid" "github.com/StevanFreeborn/advent-of-code-2025/internal/move" - "github.com/StevanFreeborn/advent-of-code-2025/internal/position" ) const PaperRollCharacter = "@" @@ -17,26 +14,18 @@ func SolvePartOne(filePath string) int { total := 0 - for row := range grid.NumberOfRows() { - for column := range grid.NumberOfColumns() { - currentPosition := position.From(row, column) - value := grid.GetValueAt(currentPosition) + for position := range grid.Positions() { + value := grid.GetValueAt(position) - if value != PaperRollCharacter { - continue - } + if value != PaperRollCharacter { + continue + } - sameNeighbors := grid.GetSameNeighborsOf(currentPosition, move.AllDirections) + sameNeighbors := grid.GetSameNeighborsOf(position, move.AllDirections) - if row == 0 && column == 3 { - fmt.Println(sameNeighbors) - } - - if len(sameNeighbors) < 4 { - total++ - } + if len(sameNeighbors) < 4 { + total++ } } - return total } diff --git a/internal/grid/grid.go b/internal/grid/grid.go index d95047b..2f214c0 100644 --- a/internal/grid/grid.go +++ b/internal/grid/grid.go @@ -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 +} diff --git a/internal/grid/grid_test.go b/internal/grid/grid_test.go index bdb6f61..21aef4f 100644 --- a/internal/grid/grid_test.go +++ b/internal/grid/grid_test.go @@ -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") + } + }) +}