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

47 lines
829 B
Go
Raw Normal View History

2024-12-14 19:11:58 +00:00
package two
import (
"bufio"
"io"
"regexp"
"strconv"
)
var reRobot = regexp.MustCompile(`p=(?P<px>\d+),(?P<py>\d+) v=(?P<vx>-?\d+),(?P<vy>-?\d+)`)
func parseLines(r io.Reader) ([]*robot, error) {
var matches [][]string
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
m := reRobot.FindStringSubmatch(line)
matches = append(matches, m)
}
if err := scanner.Err(); err != nil {
return nil, err
}
var robots []*robot
for _, m := range matches {
robots = append(robots, newRobot(mustConv(m[1]), mustConv(m[2]), mustConv(m[3]), mustConv(m[4])))
}
return robots, nil
}
func mustConv(s string) int {
n, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return n
}
func replaceAtIndex(s string, r rune, i int) string {
out := []rune(s)
out[i] = r
return string(out)
}