remove internal/util

This commit is contained in:
onyx-and-iris 2025-01-07 17:24:49 +00:00
parent c125230c9b
commit 21e942c56d
4 changed files with 61 additions and 9 deletions

View File

@ -5,13 +5,11 @@ import (
"math" "math"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/onyx-and-iris/aoc2024/day-13/internal/util"
) )
func Solve(buf []byte) (int, error) { func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf) r := bytes.NewReader(buf)
machines, err := util.ParseLines(r) machines, err := parseLines(r)
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@ -1,4 +1,4 @@
package util package one
import ( import (
"bufio" "bufio"
@ -14,7 +14,7 @@ var (
rePrize = regexp.MustCompile(`Prize: 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) { func parseLines(r io.Reader) ([]*machine.Machine, error) {
var matches [][]string var matches [][]string
scanner := bufio.NewScanner(r) scanner := bufio.NewScanner(r)
@ -47,7 +47,7 @@ func ParseLines(r io.Reader) ([]*machine.Machine, error) {
return machines, nil return machines, nil
} }
func MustConv(s string) int { func mustConv(s string) int {
n, err := strconv.Atoi(s) n, err := strconv.Atoi(s)
if err != nil { if err != nil {
panic(err) panic(err)

View File

@ -5,15 +5,13 @@ import (
"math" "math"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/onyx-and-iris/aoc2024/day-13/internal/util"
) )
const augment = 10e12 const augment = 10e12
func Solve(buf []byte) (int, error) { func Solve(buf []byte) (int, error) {
r := bytes.NewReader(buf) r := bytes.NewReader(buf)
machines, err := util.ParseLines(r) machines, err := parseLines(r)
if err != nil { if err != nil {
return 0, err return 0, err
} }

View File

@ -0,0 +1,56 @@
package two
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
}