mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 14:50:46 +00:00
43 lines
788 B
Go
43 lines
788 B
Go
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 i := 0; i < len(graph.data); i++ {
|
|
for j := 0; j < len(graph.data[i]); j++ {
|
|
start := newPoint(j, i)
|
|
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
|
|
}
|