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