feat: solve part two with iterative process

This commit is contained in:
2025-12-04 07:05:56 +01:00
parent f5bfe1e578
commit 6cff0b7931

View File

@@ -11,6 +11,17 @@ type position struct {
row, column int row, column int
} }
var directions = []position{
{-1, -1},
{-1, 0},
{-1, 1},
{0, -1},
{0, 1},
{1, -1},
{1, 0},
{1, 1},
}
func init() { func init() {
registry.Register("2025D4", ParseInput, PartOne, PartTwo) registry.Register("2025D4", ParseInput, PartOne, PartTwo)
} }
@@ -32,17 +43,6 @@ func PartOne(data []string) int {
} }
accessibleRolls := 0 accessibleRolls := 0
directions := []position{
{-1, -1},
{-1, 0},
{-1, 1},
{0, -1},
{0, 1},
{1, -1},
{1, 0},
{1, 1},
}
for pos := range isRoll { for pos := range isRoll {
adjacentRolls := 0 adjacentRolls := 0
for _, direction := range directions { for _, direction := range directions {
@@ -61,5 +61,44 @@ func PartOne(data []string) int {
} }
func PartTwo(data []string) int { func PartTwo(data []string) int {
return 0 isRoll := make(map[position]bool)
for row := range data {
for column := 0; column < len(data[row]); column++ {
if data[row][column] == '@' {
isRoll[position{row, column}] = true
}
}
}
totalRemoved := 0
for {
accessibleRolls := make(map[position]bool)
for pos := range isRoll {
adjacentRolls := 0
for _, direction := range directions {
neighbor := position{pos.row + direction.row, pos.column + direction.column}
if isRoll[neighbor] {
adjacentRolls++
}
}
if adjacentRolls < 4 {
accessibleRolls[pos] = true
}
}
if len(accessibleRolls) == 0 {
break
}
for pos := range accessibleRolls {
delete(isRoll, pos)
totalRemoved++
}
}
return totalRemoved
} }