Files
advent-of-code-2025/cmd/04/main.go
T

69 lines
1.3 KiB
Go
Raw Normal View History

2025-12-07 07:58:26 -06:00
package main
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"
2025-12-08 05:39:47 -06:00
"github.com/StevanFreeborn/advent-of-code-2025/internal/position"
2025-12-07 07:58:26 -06:00
)
const PaperRollCharacter = "@"
2025-12-08 05:39:47 -06:00
const EmptySpaceCharacter = "."
2025-12-07 07:58:26 -06:00
func SolvePartOne(filePath string) int {
input := file.ReadAllLines(filePath)
grid := grid.From(input)
2025-12-07 07:58:26 -06:00
total := 0
for position := range grid.Positions() {
value := grid.GetValueAt(position)
2025-12-07 07:58:26 -06:00
if value != PaperRollCharacter {
continue
}
2025-12-07 07:58:26 -06:00
sameNeighbors := grid.GetSameNeighborsOf(position, move.AllDirections)
2025-12-07 07:58:26 -06:00
if len(sameNeighbors) < 4 {
total++
2025-12-07 07:58:26 -06:00
}
}
2025-12-08 05:39:47 -06:00
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)
}
2025-12-07 07:58:26 -06:00
return total
}