mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 14:50:46 +00:00
47 lines
829 B
Go
47 lines
829 B
Go
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)
|
|
}
|