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

71 lines
1.2 KiB
Go
Raw Normal View History

2024-12-18 14:45:27 +00:00
package two
import (
"bufio"
"io"
"strings"
log "github.com/sirupsen/logrus"
)
func parseLines(r io.Reader) (*graph, string, error) {
graph := newGraph()
var directions []string
var sb strings.Builder
var inDirections bool
var linecount int
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
if len(line) == 0 {
inDirections = true
continue
}
if inDirections {
directions = append(directions, line)
} else {
for _, r := range line {
switch r {
case '#':
sb.WriteRune('#')
sb.WriteRune('#')
case 'O':
sb.WriteRune('[')
sb.WriteRune(']')
case '@':
sb.WriteRune('@')
sb.WriteRune('.')
default:
sb.WriteRune('.')
sb.WriteRune('.')
}
}
indx := strings.Index(sb.String(), "@")
if indx != -1 {
log.Debugf("adding start point at (%d, %d)", indx, linecount)
graph.robot = newPoint(indx, linecount)
}
graph.data = append(graph.data, sb.String())
sb.Reset()
}
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)
}