aoc2024/day-15/internal/one/util.go

60 lines
1.1 KiB
Go
Raw Permalink Normal View History

2024-12-16 11:56:53 +00:00
package one
import (
"bufio"
"io"
"regexp"
"strings"
log "github.com/sirupsen/logrus"
)
var reBoxes = regexp.MustCompile(`O`)
func parseLines(r io.Reader) (*graph, string, error) {
graph := newGraph()
var directions []string
var inDirections bool
var linecount int
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
if len(line) == 0 {
inDirections = true
continue
}
matches := reBoxes.FindAllStringIndex(line, -1)
for _, m := range matches {
graph.boxes[newPoint(m[0], linecount)] = struct{}{}
}
if inDirections {
directions = append(directions, line)
} else {
indx := strings.Index(line, "@")
if indx != -1 {
log.Debugf("adding start point at (%d, %d)", indx, linecount)
graph.robot = newPoint(indx, linecount)
}
graph.data = append(graph.data, line)
}
linecount++
}
if err := scanner.Err(); err != nil {
return nil, "", err
}
return graph, strings.Join(directions, ""), nil
}
func replaceAtIndex(s string, r rune, i int) string {
out := []rune(s)
out[i] = r
return string(out)
}