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

53 lines
841 B
Go
Raw Permalink Normal View History

2024-12-19 01:27:45 +00:00
package two
import (
"bufio"
"io"
"strings"
)
func parseLines(r io.Reader) (*graph, error) {
graph := newGraph()
var linecount int
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
r, indx := indxForAny(line, []rune{'S', 'E'})
if indx != -1 {
if r == 'S' {
graph.start = newNode(indx, linecount, E)
} else {
graph.end = newNode(indx, linecount, 0)
}
}
graph.data = append(graph.data, line)
linecount++
}
if err := scanner.Err(); err != nil {
return nil, err
}
return graph, nil
}
func indxForAny(s string, runes []rune) (rune, int) {
for _, r := range runes {
indx := strings.Index(s, string(r))
if indx != -1 {
return r, indx
}
}
return 0, -1
}
func replaceAtIndex(s string, r rune, i int) string {
out := []rune(s)
out[i] = r
return string(out)
}