feat: solve PartOne using DFS algorithm
This commit is contained in:
73
internal/2025/DaySeven/code.go
Normal file
73
internal/2025/DaySeven/code.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package dayseven
|
||||
|
||||
import (
|
||||
"advent-of-code/internal/registry"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.Register("2025D7", ParseInput, PartOne, PartTwo)
|
||||
}
|
||||
|
||||
type Position struct {
|
||||
row int
|
||||
column int
|
||||
}
|
||||
|
||||
func ParseInput(filepath string) []string {
|
||||
content, _ := os.ReadFile(filepath)
|
||||
return strings.Split(string(content), "\n")
|
||||
}
|
||||
|
||||
func findStart(input []string) (int, int) {
|
||||
for row := range input {
|
||||
if column := strings.IndexByte(input[row], 'S'); column != -1 {
|
||||
return row, column
|
||||
}
|
||||
}
|
||||
return -1, -1
|
||||
}
|
||||
|
||||
func PartOne(input []string) int {
|
||||
rows, columns := len(input), len(input[0])
|
||||
|
||||
startRow, startColumn := findStart(input)
|
||||
|
||||
queue := []Position{{startRow, startColumn}}
|
||||
activated := make(map[Position]bool)
|
||||
splits := 0
|
||||
|
||||
for len(queue) > 0 {
|
||||
current := queue[0]
|
||||
queue = queue[1:]
|
||||
|
||||
row := current.row + 1
|
||||
column := current.column
|
||||
|
||||
for row < rows && input[row][column] != '^' {
|
||||
row++
|
||||
}
|
||||
|
||||
if row < rows {
|
||||
splitPosition := Position{row, column}
|
||||
if !activated[splitPosition] {
|
||||
activated[splitPosition] = true
|
||||
splits++
|
||||
|
||||
if column > 0 {
|
||||
queue = append(queue, Position{row, column - 1})
|
||||
}
|
||||
if column < columns-1 {
|
||||
queue = append(queue, Position{row, column + 1})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return splits
|
||||
}
|
||||
|
||||
func PartTwo(data []string) int {
|
||||
return 0
|
||||
}
|
||||
Reference in New Issue
Block a user