mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-09 22:30:47 +00:00
53 lines
841 B
Go
53 lines
841 B
Go
package one
|
|
|
|
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)
|
|
}
|