mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 14:50:46 +00:00
60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
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)
|
|
}
|