2024-12-08 19:55:23 +00:00
|
|
|
package two
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
|
|
|
var reMatchAntennae = regexp.MustCompile(`[a-zA-Z0-9]`)
|
|
|
|
|
|
|
|
func parseLines(r io.Reader) (*graph, error) {
|
|
|
|
graph := newGraph()
|
|
|
|
|
|
|
|
var linecount int
|
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
graph.data = append(graph.data, line)
|
|
|
|
|
|
|
|
for _, m := range reMatchAntennae.FindAllStringIndex(line, -1) {
|
|
|
|
graph.antennae = append(
|
|
|
|
graph.antennae,
|
|
|
|
newAntenna(m[0], linecount, rune(line[m[0]])),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
linecount++
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return graph, nil
|
|
|
|
}
|
2025-01-07 17:24:01 +00:00
|
|
|
|
|
|
|
func replaceAtIndex(s string, r rune, i int) string {
|
|
|
|
out := []rune(s)
|
|
|
|
out[i] = r
|
|
|
|
return string(out)
|
|
|
|
}
|