package util import ( "bufio" "io" "regexp" "strconv" "github.com/onyx-and-iris/aoc2024/day-13/internal/machine" ) var ( reButton = regexp.MustCompile(`Button (?P[A|B]): X\+(?P\d+), Y\+(?P\d+)`) rePrize = regexp.MustCompile(`Prize: X\=(?P\d+), Y\=(?P\d+)`) ) func ParseLines(r io.Reader) ([]*machine.Machine, error) { var matches [][]string scanner := bufio.NewScanner(r) for scanner.Scan() { line := scanner.Text() if len(line) == 0 { continue } var m []string switch line := line; { case reButton.MatchString(line): m = reButton.FindStringSubmatch(line) case rePrize.MatchString(line): m = rePrize.FindStringSubmatch(line) } matches = append(matches, m) } if err := scanner.Err(); err != nil { return nil, err } var machines []*machine.Machine for i := 0; i < len(matches); i += 3 { machines = append(machines, machine.New(matches[i], matches[i+1], matches[i+2])) } return machines, nil } func MustConv(s string) int { n, err := strconv.Atoi(s) if err != nil { panic(err) } return n }