diff --git a/day-03/internal/one/solve.go b/day-03/internal/one/solve.go index ed6824c..92a2ac1 100644 --- a/day-03/internal/one/solve.go +++ b/day-03/internal/one/solve.go @@ -9,7 +9,7 @@ import ( "github.com/onyx-and-iris/aoc2024/day-03/internal/util" ) -var reMul = regexp.MustCompile(`mul\((?P[0-9]{1,3}),(?P[0-9]{1,3})\)`) +var reMul = regexp.MustCompile(`mul\(([0-9]{1,3}),([0-9]{1,3})\)`) func Solve(data []byte) (int, error) { r := bytes.NewReader(data) @@ -26,10 +26,9 @@ func parseLines(r io.Reader) (int, error) { scanner := bufio.NewScanner(r) for scanner.Scan() { - m := reMul.FindAllString(scanner.Text(), -1) - for _, v := range m { - groups := util.GetGroups(reMul, v) - sum += util.MustConv(groups["first"]) * util.MustConv(groups["second"]) + matches := reMul.FindAllStringSubmatch(scanner.Text(), -1) + for _, m := range matches { + sum += util.MustConv(m[1]) * util.MustConv(m[2]) } } diff --git a/day-03/internal/two/solve.go b/day-03/internal/two/solve.go index c3132a4..2a1e3a5 100644 --- a/day-03/internal/two/solve.go +++ b/day-03/internal/two/solve.go @@ -11,7 +11,7 @@ import ( log "github.com/sirupsen/logrus" ) -var reMulOrDoOrDont = regexp.MustCompile(`mul\((?P[0-9]{1,3}),(?P[0-9]{1,3})\)|do\(\)|don't\(\)`) +var reMulOrDoOrDont = regexp.MustCompile(`mul\(([0-9]{1,3}),([0-9]{1,3})\)|do\(\)|don't\(\)`) func Solve(data []byte) (int, error) { r := bytes.NewReader(data) @@ -29,9 +29,9 @@ func parseLines(r io.Reader) (int, error) { scanner := bufio.NewScanner(r) for scanner.Scan() { - m := reMulOrDoOrDont.FindAllString(scanner.Text(), -1) - for _, v := range m { - switch v { + matches := reMulOrDoOrDont.FindAllStringSubmatch(scanner.Text(), -1) + for _, m := range matches { + switch m[0] { case "do()": do = true log.Debug("hit a do") @@ -41,10 +41,9 @@ func parseLines(r io.Reader) (int, error) { } if do { - groups := util.GetGroups(reMulOrDoOrDont, v) - if groups["first"] != "" && groups["second"] != "" { - log.Debugf("%s * %s\n", groups["first"], groups["second"]) - sum += util.MustConv(groups["first"]) * util.MustConv(groups["second"]) + if m[1] != "" && m[2] != "" { + log.Debugf("%s * %s\n", m[1], m[2]) + sum += util.MustConv(m[1]) * util.MustConv(m[2]) } } } diff --git a/day-03/internal/util/util.go b/day-03/internal/util/util.go index 35c11e3..b02895e 100644 --- a/day-03/internal/util/util.go +++ b/day-03/internal/util/util.go @@ -1,21 +1,9 @@ package util import ( - "regexp" "strconv" ) -func GetGroups(r *regexp.Regexp, s string) map[string]string { - groups := make(map[string]string) - names := r.SubexpNames() - for i, res := range r.FindStringSubmatch(s) { - if i != 0 { - groups[names[i]] = res - } - } - return groups -} - func MustConv(s string) int { n, err := strconv.Atoi(s) if err != nil {