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{})
|
2025-01-08 05:42:35 +00:00
|
|
|
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
|
|
|
|
}
|