Advent-of-Code/year25/day4.go
2025-12-04 09:02:48 +02:00

79 lines
1.3 KiB
Go

package year25
import (
"fmt"
"io"
"git.bizdoc.ro/private/devkit.git/collections/geometry/v2"
)
const Day4PaperRoll = '@'
func Day4Part1(r io.Reader, l Logger) (any, error) {
// 4 rolls in 8 adjacent positions
var accessibleRolls int
g, err := geometry.ReaderToByteGrid(r)
if err != nil {
return nil, fmt.Errorf("day4: failed to read grid: %w", err)
}
for c := range g.Points() {
if g.At(c) != Day4PaperRoll {
continue
}
var count int
for p := range g.SubgridPointsIter(c.MoveUpLeft(), c.MoveDownRight()) {
if g.At(p) == Day4PaperRoll {
count += 1
}
}
if count <= 4 {
accessibleRolls += 1
}
}
return accessibleRolls, nil
}
func Day4Part2(r io.Reader, l Logger) (any, error) {
// 4 rolls in 8 adjacent positions
var totalRemovedRolls int
g, err := geometry.ReaderToByteGrid(r)
if err != nil {
return nil, fmt.Errorf("day4: failed to read grid: %w", err)
}
for {
removedRolls := 0
for c := range g.Points() {
if g.At(c) != Day4PaperRoll {
continue
}
var count int
for p := range g.SubgridPointsIter(c.MoveUpLeft(), c.MoveDownRight()) {
if g.At(p) == Day4PaperRoll {
count += 1
}
}
if count <= 4 {
removedRolls += 1
g.Set(c, '.') // remove grid
}
}
if removedRolls == 0 {
break
}
totalRemovedRolls += removedRolls
}
return totalRemovedRolls, nil
}