refactor: add positions method to grid and use that for iterating in day 4 part 1
This commit is contained in:
+8
-19
@@ -1,12 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/StevanFreeborn/advent-of-code-2025/internal/file"
|
"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/grid"
|
||||||
"github.com/StevanFreeborn/advent-of-code-2025/internal/move"
|
"github.com/StevanFreeborn/advent-of-code-2025/internal/move"
|
||||||
"github.com/StevanFreeborn/advent-of-code-2025/internal/position"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const PaperRollCharacter = "@"
|
const PaperRollCharacter = "@"
|
||||||
@@ -17,26 +14,18 @@ func SolvePartOne(filePath string) int {
|
|||||||
|
|
||||||
total := 0
|
total := 0
|
||||||
|
|
||||||
for row := range grid.NumberOfRows() {
|
for position := range grid.Positions() {
|
||||||
for column := range grid.NumberOfColumns() {
|
value := grid.GetValueAt(position)
|
||||||
currentPosition := position.From(row, column)
|
|
||||||
value := grid.GetValueAt(currentPosition)
|
|
||||||
|
|
||||||
if value != PaperRollCharacter {
|
if value != PaperRollCharacter {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
sameNeighbors := grid.GetSameNeighborsOf(currentPosition, move.AllDirections)
|
sameNeighbors := grid.GetSameNeighborsOf(position, move.AllDirections)
|
||||||
|
|
||||||
if row == 0 && column == 3 {
|
if len(sameNeighbors) < 4 {
|
||||||
fmt.Println(sameNeighbors)
|
total++
|
||||||
}
|
|
||||||
|
|
||||||
if len(sameNeighbors) < 4 {
|
|
||||||
total++
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return total
|
return total
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ type Grid interface {
|
|||||||
InBounds(position.Position) bool
|
InBounds(position.Position) bool
|
||||||
GetValueAt(position.Position) string
|
GetValueAt(position.Position) string
|
||||||
GetSameNeighborsOf(position.Position, []move.Move) []position.Position
|
GetSameNeighborsOf(position.Position, []move.Move) []position.Position
|
||||||
|
Positions() map[position.Position]string
|
||||||
}
|
}
|
||||||
|
|
||||||
type grid struct {
|
type grid struct {
|
||||||
@@ -95,3 +96,14 @@ func (g grid) GetSameNeighborsOf(pos position.Position, moves []move.Move) []pos
|
|||||||
|
|
||||||
return similarNeighbors
|
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
|
package grid_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"maps"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/StevanFreeborn/advent-of-code-2025/internal/grid"
|
"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