mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 06:40:47 +00:00
57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
package util
|
|
|
|
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)
|
|
}
|
|
|
|
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]))
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return machines, nil
|
|
}
|
|
|
|
func MustConv(s string) int {
|
|
n, err := strconv.Atoi(s)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return n
|
|
}
|