mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2025-01-10 14:50:46 +00:00
42 lines
737 B
Go
42 lines
737 B
Go
|
package one
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"bytes"
|
||
|
"io"
|
||
|
"regexp"
|
||
|
|
||
|
"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})\)`)
|
||
|
|
||
|
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() {
|
||
|
m := reMul.FindAllString(scanner.Text(), -1)
|
||
|
for _, v := range m {
|
||
|
groups := util.GetGroups(reMul, v)
|
||
|
sum += util.MustConv(groups["first"]) * util.MustConv(groups["second"])
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if err := scanner.Err(); err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
|
||
|
return sum, nil
|
||
|
}
|