package one import ( "bufio" "bytes" "io" "regexp" "github.com/onyx-and-iris/aoc2024/day-03/internal/util" ) var reMul = regexp.MustCompile(`mul\(([0-9]{1,3}),([0-9]{1,3})\)`) func Solve(data []byte) (int, error) { r := bytes.NewReader(data) sum, err := parseLines(r) if err != nil { return 0, err } return sum, nil } func parseLines(r io.Reader) (int, error) { var sum int scanner := bufio.NewScanner(r) for scanner.Scan() { matches := reMul.FindAllStringSubmatch(scanner.Text(), -1) for _, m := range matches { sum += util.MustConv(m[1]) * util.MustConv(m[2]) } } if err := scanner.Err(); err != nil { return 0, err } return sum, nil }