-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.go
50 lines (40 loc) · 975 Bytes
/
day3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// [Day 3: Mull It Over](https://adventofcode.com/2024/day/3)
package main
import (
"fmt"
"os"
"regexp"
"strconv"
)
// solve computes valid mul() operations.
// If part1 is false, obey to the do()/don't() statements.
func solve(data string, part1 bool) int64 {
var enabled bool = true
var totalSum int64
re := regexp.MustCompile(`mul\((\d+),(\d+)\)|do\(\)|don't\(\)`)
for _, m := range re.FindAllStringSubmatch(data, -1) {
if m[0] == "do()" {
enabled = true
} else if m[0] == "don't()" {
enabled = false
} else if enabled || part1 {
x, _ := strconv.ParseInt(m[1], 10, 0)
y, _ := strconv.ParseInt(m[2], 10, 0)
totalSum += x * y
}
}
return totalSum
}
func main() {
inputFile := "input.txt"
if len(os.Args) >= 2 {
inputFile = os.Args[1]
}
data, err := os.ReadFile(inputFile)
if err != nil {
fmt.Println("Error reading file:", err)
return
}
fmt.Println(solve(string(data), true))
fmt.Println(solve(string(data), false))
}