This commit is contained in:
Gabriel Bizdoc 2025-12-04 09:02:48 +02:00
parent 2834ad59ee
commit b04ae5c077
No known key found for this signature in database
GPG Key ID: 3F0EDAECA5BE9ED9
3 changed files with 86 additions and 0 deletions

4
go.mod
View File

@ -1,3 +1,7 @@
module git.bizdoc.ro/gabi-public/Advent-of-Code.git module git.bizdoc.ro/gabi-public/Advent-of-Code.git
go 1.25.1 go 1.25.1
require git.bizdoc.ro/private/devkit.git v0.0.0-20250902190935-21cc248cb135
require golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect

4
go.sum Normal file
View File

@ -0,0 +1,4 @@
git.bizdoc.ro/private/devkit.git v0.0.0-20250902190935-21cc248cb135 h1:mUorxNlLlyvmPuZMKP80RFJVRcI1j85u1g8TDPht7AM=
git.bizdoc.ro/private/devkit.git v0.0.0-20250902190935-21cc248cb135/go.mod h1:r/tE3YYNlrYNQc9DF4ph2Ur5WYKCtaqOM+8jqvzSoAQ=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak=

78
year25/day4.go Normal file
View File

@ -0,0 +1,78 @@
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
}