mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2026-04-09 02:23:36 +00:00
remove internal/util
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
56
day-13/internal/one/util.go
Normal file
56
day-13/internal/one/util.go
Normal file
@@ -0,0 +1,56 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user