diff --git a/go.mod b/go.mod index 2e9fb00..68736f2 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6032aba --- /dev/null +++ b/go.sum @@ -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= diff --git a/year25/day4.go b/year25/day4.go new file mode 100644 index 0000000..ba39269 --- /dev/null +++ b/year25/day4.go @@ -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 +}