aoc2024/day-13/internal/one/util.go

57 lines
1.1 KiB
Go

package one
import (
"bufio"
"io"
"regexp"
"strconv"
"github.com/onyx-and-iris/aoc2024/day-13/internal/machine"
)
var (
reButton = regexp.MustCompile(`Button (?P<identifier>[A|B]): X\+(?P<xdigit>\d+), Y\+(?P<ydigit>\d+)`)
rePrize = regexp.MustCompile(`Prize: X\=(?P<xdigit>\d+), Y\=(?P<ydigit>\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
}