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

57 lines
1.1 KiB
Go
Raw Permalink Normal View History

2025-01-07 17:24:49 +00:00
package one
2024-12-13 12:11:56 +00:00
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+)`)
)
2025-01-07 17:24:49 +00:00
func parseLines(r io.Reader) ([]*machine.Machine, error) {
2024-12-13 12:11:56 +00:00
var matches [][]string
scanner := bufio.NewScanner(r)
for scanner.Scan() {
2024-12-13 14:47:56 +00:00
line := scanner.Text()
if len(line) == 0 {
2024-12-13 12:11:56 +00:00
continue
}
var m []string
2024-12-13 14:47:56 +00:00
switch line := line; {
case reButton.MatchString(line):
2024-12-13 12:11:56 +00:00
m = reButton.FindStringSubmatch(line)
2024-12-13 14:47:56 +00:00
case rePrize.MatchString(line):
2024-12-13 12:11:56 +00:00
m = rePrize.FindStringSubmatch(line)
}
matches = append(matches, m)
}
2024-12-13 19:29:32 +00:00
if err := scanner.Err(); err != nil {
return nil, err
}
2024-12-13 12:11:56 +00:00
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
}
2025-01-07 17:24:49 +00:00
func mustConv(s string) int {
2024-12-13 12:11:56 +00:00
n, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return n
}