Advent-of-Code/year25/day4.go

81 lines
1.5 KiB
Go

package year25
import (
"fmt"
"git.bizdoc.ro/gabi-public/Advent-of-Code.git/aoc"
"git.bizdoc.ro/private/devkit.git/collections/geometry/v2"
)
const Day4PaperRoll = '@'
func Day4Part1(ctx aoc.Context) (any, error) {
// 4 rolls in 8 adjacent positions
const maxAdjacentRolls = 4
var accessibleRolls int
g, err := geometry.ReaderToByteGrid(ctx.Body)
if err != nil {
return nil, fmt.Errorf("day4 part1: 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 <= maxAdjacentRolls {
accessibleRolls += 1
}
}
return accessibleRolls, nil
}
func Day4Part2(ctx aoc.Context) (any, error) {
// 4 rolls in 8 adjacent positions
const maxAdjacentRolls = 4
var totalRemovedRolls int
g, err := geometry.ReaderToByteGrid(ctx.Body)
if err != nil {
return nil, fmt.Errorf("day4 part2: 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 <= maxAdjacentRolls {
removedRolls += 1
g.Set(c, '.') // remove roll from grid
}
}
if removedRolls == 0 {
break
}
totalRemovedRolls += removedRolls
}
return totalRemovedRolls, nil
}