You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
851 B
49 lines
851 B
package adventoc2024
|
|
|
|
import (
|
|
"io"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
type Day3 struct{}
|
|
|
|
func (d Day3) Part1(r io.Reader) string {
|
|
var bd strings.Builder
|
|
var sum int
|
|
io.Copy(&bd, r)
|
|
|
|
re := regexp.MustCompile(`mul\((\d{1,3}),(\d{1,3})\)`)
|
|
|
|
matches := re.FindAllStringSubmatch(bd.String(), -1)
|
|
|
|
for _, match := range matches {
|
|
sum += atoi(match[1]) * atoi(match[2])
|
|
}
|
|
return printi(sum)
|
|
}
|
|
|
|
func (d Day3) Part2(r io.Reader) string {
|
|
var bd strings.Builder
|
|
var sum int
|
|
io.Copy(&bd, r)
|
|
|
|
re := regexp.MustCompile(`mul\((\d{1,3}),(\d{1,3})\)|do\(\)|don\'t\(\)`)
|
|
|
|
matches := re.FindAllStringSubmatch(bd.String(), -1)
|
|
|
|
var skip bool
|
|
for _, match := range matches {
|
|
switch match[0] {
|
|
case "do()":
|
|
skip = false
|
|
case "don't()":
|
|
skip = true
|
|
default:
|
|
if !skip {
|
|
sum += atoi(match[1]) * atoi(match[2])
|
|
}
|
|
}
|
|
}
|
|
return printi(sum)
|
|
}
|
|
|