day 4
This commit is contained in:
parent
2834ad59ee
commit
b04ae5c077
4
go.mod
4
go.mod
@ -1,3 +1,7 @@
|
||||
module git.bizdoc.ro/gabi-public/Advent-of-Code.git
|
||||
|
||||
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
4
go.sum
Normal 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
78
year25/day4.go
Normal 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
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user