feat: solve day 4 part 2
This commit is contained in:
@@ -4,9 +4,11 @@ import (
|
||||
"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 = "@"
|
||||
const EmptySpaceCharacter = "."
|
||||
|
||||
func SolvePartOne(filePath string) int {
|
||||
input := file.ReadAllLines(filePath)
|
||||
@@ -27,5 +29,40 @@ func SolvePartOne(filePath string) int {
|
||||
total++
|
||||
}
|
||||
}
|
||||
|
||||
return total
|
||||
}
|
||||
|
||||
func SolvePartTwo(filePath string) int {
|
||||
input := file.ReadAllLines(filePath)
|
||||
g := grid.From(input)
|
||||
|
||||
total := 0
|
||||
|
||||
for {
|
||||
positionsToRemove := []position.Position{}
|
||||
|
||||
for position := range g.Positions() {
|
||||
value := g.GetValueAt(position)
|
||||
|
||||
if value != PaperRollCharacter {
|
||||
continue
|
||||
}
|
||||
|
||||
sameNeighbors := g.GetSameNeighborsOf(position, move.AllDirections)
|
||||
|
||||
if len(sameNeighbors) < 4 {
|
||||
positionsToRemove = append(positionsToRemove, position)
|
||||
total++
|
||||
}
|
||||
}
|
||||
|
||||
if len(positionsToRemove) == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
g.SetValuesAt(positionsToRemove, EmptySpaceCharacter)
|
||||
}
|
||||
|
||||
return total
|
||||
}
|
||||
|
||||
@@ -25,3 +25,23 @@ func TestSolvePartOneWithInput(t *testing.T) {
|
||||
t.Errorf("got %d but wanted %d", result, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSolvePartTwoWithExampleInput(t *testing.T) {
|
||||
expected := 43
|
||||
|
||||
result := solution.SolvePartTwo("EXAMPLE.txt")
|
||||
|
||||
if result != expected {
|
||||
t.Errorf("got %d but wanted %d", result, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSolvePartTwoWithInput(t *testing.T) {
|
||||
expected := 8451
|
||||
|
||||
result := solution.SolvePartTwo("INPUT.txt")
|
||||
|
||||
if result != expected {
|
||||
t.Errorf("got %d but wanted %d", result, expected)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user