Skip to content

Commit 91821de

Browse files
committed
feat(2024): refactor day 3
Refactored Day 3 of 2024 to use the common framework. Successfully migrated solution and integrated it into the runner.
1 parent 4fc0c72 commit 91821de

3 files changed

Lines changed: 57 additions & 0 deletions

File tree

2024/golang/cmd/day03.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"strconv"
7+
8+
"github.com/markkovari/advent_of_code/aoc-go-common"
9+
)
10+
11+
type Day03 struct{}
12+
13+
func (d Day03) Part1(input string) string {
14+
pattern := regexp.MustCompile(`mul\((\d+),(\d+)\)`)
15+
matches := pattern.FindAllStringSubmatch(input, -1)
16+
var sum int64
17+
for _, m := range matches {
18+
sum += toInt(m[1]) * toInt(m[2])
19+
}
20+
return fmt.Sprintf("%d", sum)
21+
}
22+
23+
func (d Day03) Part2(input string) string {
24+
pattern := regexp.MustCompile(`mul\((\d+),(\d+)\)|do\(\)|don't\(\)`)
25+
matches := pattern.FindAllStringSubmatch(input, -1)
26+
var sum int64
27+
enabled := true
28+
for _, m := range matches {
29+
switch m[0] {
30+
case "do()":
31+
enabled = true
32+
case "don't()":
33+
enabled = false
34+
default:
35+
if enabled {
36+
sum += toInt(m[1]) * toInt(m[2])
37+
}
38+
}
39+
}
40+
return fmt.Sprintf("%d", sum)
41+
}
42+
43+
func toInt(s string) int64 {
44+
val, _ := strconv.ParseInt(s, 10, 64)
45+
return val
46+
}
47+
48+
func main() {
49+
common.Run(2024, 3, Day03{})
50+
}

0 commit comments

Comments
 (0)