diff --git a/day-13/internal/one/solve.go b/day-13/internal/one/solve.go index 1e9f188..5b59cbb 100644 --- a/day-13/internal/one/solve.go +++ b/day-13/internal/one/solve.go @@ -5,13 +5,11 @@ import ( "math" log "github.com/sirupsen/logrus" - - "github.com/onyx-and-iris/aoc2024/day-13/internal/util" ) func Solve(buf []byte) (int, error) { r := bytes.NewReader(buf) - machines, err := util.ParseLines(r) + machines, err := parseLines(r) if err != nil { return 0, err } diff --git a/day-13/internal/util/util.go b/day-13/internal/one/util.go similarity index 90% rename from day-13/internal/util/util.go rename to day-13/internal/one/util.go index 08b3fe4..431bf30 100644 --- a/day-13/internal/util/util.go +++ b/day-13/internal/one/util.go @@ -1,4 +1,4 @@ -package util +package one import ( "bufio" @@ -14,7 +14,7 @@ var ( rePrize = regexp.MustCompile(`Prize: X\=(?P\d+), Y\=(?P\d+)`) ) -func ParseLines(r io.Reader) ([]*machine.Machine, error) { +func parseLines(r io.Reader) ([]*machine.Machine, error) { var matches [][]string scanner := bufio.NewScanner(r) @@ -47,7 +47,7 @@ func ParseLines(r io.Reader) ([]*machine.Machine, error) { return machines, nil } -func MustConv(s string) int { +func mustConv(s string) int { n, err := strconv.Atoi(s) if err != nil { panic(err) diff --git a/day-13/internal/two/solve.go b/day-13/internal/two/solve.go index 73ea9f3..2193c28 100644 --- a/day-13/internal/two/solve.go +++ b/day-13/internal/two/solve.go @@ -5,15 +5,13 @@ import ( "math" log "github.com/sirupsen/logrus" - - "github.com/onyx-and-iris/aoc2024/day-13/internal/util" ) const augment = 10e12 func Solve(buf []byte) (int, error) { r := bytes.NewReader(buf) - machines, err := util.ParseLines(r) + machines, err := parseLines(r) if err != nil { return 0, err } diff --git a/day-13/internal/two/util.go b/day-13/internal/two/util.go new file mode 100644 index 0000000..6ba8ab8 --- /dev/null +++ b/day-13/internal/two/util.go @@ -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[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 +}