feat: solve day 4 part 2

This commit is contained in:
Stevan Freeborn
2025-12-08 05:39:47 -06:00
parent 801b492e59
commit 0b74bfd821
4 changed files with 89 additions and 1 deletions
+31
View File
@@ -1,6 +1,8 @@
package grid
import (
"strings"
"github.com/StevanFreeborn/advent-of-code-2025/internal/move"
"github.com/StevanFreeborn/advent-of-code-2025/internal/position"
)
@@ -12,6 +14,7 @@ type Grid interface {
GetValueAt(position.Position) string
GetSameNeighborsOf(position.Position, []move.Move) []position.Position
Positions() map[position.Position]string
SetValuesAt(positions []position.Position, value string)
}
type grid struct {
@@ -20,6 +23,10 @@ type grid struct {
positions map[position.Position]string
}
func New(positions map[position.Position]string) Grid {
return grid{positions: positions}
}
func From(input []string) Grid {
numberOfRows := len(input)
numberOfColumns := len(input[0])
@@ -95,3 +102,27 @@ func (g grid) Positions() map[position.Position]string {
return positions
}
func (g grid) SetValuesAt(positions []position.Position, value string) {
for _, pos := range positions {
g.positions[pos] = value
}
}
func (g grid) String() string {
var rowString strings.Builder
for row := range g.numberOfRows {
var colString strings.Builder
for col := range g.numberOfColumns {
pos := position.From(row, col)
value := g.positions[pos]
colString.WriteString(value)
}
rowString.WriteString(colString.String() + "\n")
}
return rowString.String()
}