mirror of
				https://github.com/onyx-and-iris/aoc2024.git
				synced 2025-11-03 22:41:46 +00:00 
			
		
		
		
	get rid of named match groups
This commit is contained in:
		
							parent
							
								
									d32b376619
								
							
						
					
					
						commit
						fca32422e4
					
				@ -9,7 +9,7 @@ import (
 | 
			
		||||
	"github.com/onyx-and-iris/aoc2024/day-03/internal/util"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var reMul = regexp.MustCompile(`mul\((?P<first>[0-9]{1,3}),(?P<second>[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])
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -11,7 +11,7 @@ import (
 | 
			
		||||
	log "github.com/sirupsen/logrus"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var reMulOrDoOrDont = regexp.MustCompile(`mul\((?P<first>[0-9]{1,3}),(?P<second>[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])
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
@ -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 {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user