Browse Source

solve day 3 part 2

master
Alexander Avery 3 weeks ago
parent
commit
2689562ac2
  1. 23
      2024/day03.go

23
2024/day03.go

@ -24,5 +24,26 @@ func (d Day3) Part1(r io.Reader) string {
}
func (d Day3) Part2(r io.Reader) string {
return ""
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)
}

Loading…
Cancel
Save