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

38 lines
646 B
Go
Raw Normal View History

2024-12-07 23:39:28 +00:00
package two
import (
"bufio"
"io"
"strconv"
"strings"
)
func mustConv(s string) int {
n, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return n
}
func parseLines(r io.Reader) ([]equation, error) {
equations := []equation{}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
parts := strings.Split(scanner.Text(), ":")
res := mustConv(parts[0])
var operands []int
for _, s := range strings.Fields(parts[1]) {
operands = append(operands, mustConv(string(s)))
}
equations = append(equations, newEquation(res, operands))
}
if err := scanner.Err(); err != nil {
return nil, err
}
return equations, nil
}