Files
advent-of-code-2024/day3/mul.go

37 lines
671 B
Go
Raw Normal View History

2024-12-03 09:47:45 +01:00
package main
import (
"fmt"
"os"
"regexp"
"strconv"
)
func check(e error) {
if e != nil { panic(e) }
}
func main() {
dat, err := os.ReadFile("data.txt")
check(err)
dat_str := string(dat)
// get all numbers within mul(*,*) using capture groups
r := regexp.MustCompile("mul\\(([0-9]{1,3}),([0-9]{1,3})\\)")
matches := r.FindAllStringSubmatch(dat_str, -1)
acc := 0
for _, elem := range matches {
// convert to numbers
num1, err := strconv.Atoi(elem[1])
check(err)
num2, err := strconv.Atoi(elem[2])
check(err)
acc += num1 * num2
}
fmt.Println(acc)
}