aoc2024/day-12/internal/one/solve.go

43 lines
766 B
Go
Raw Permalink Normal View History

2024-12-13 10:02:48 +00:00
package one
import (
"bytes"
log "github.com/sirupsen/logrus"
)
func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf)
graph, err := parseLines(r)
if err != nil {
return 0, err
}
var totalCost int
totalAreaVisited := make(map[point]struct{})
for y := range graph.data {
for x := range graph.data[y] {
start := newPoint(x, y)
2024-12-13 10:02:48 +00:00
if graph.isOutOfBounds(start) {
continue
}
_, ok := totalAreaVisited[start]
if ok {
continue
}
totalAreaVisited[start] = struct{}{}
path := explore(start, graph)
log.Debugf("\n%s\n", graph.debug(path.visited))
totalCost += len(path.visited) * path.perimeter
for point := range path.visited {
totalAreaVisited[point] = struct{}{}
}
}
}
return totalCost, nil
}