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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
package main
import (
"fmt"
"os"
"regexp"
"strconv"
)
func FileRead(path string) string {
file, err := os.ReadFile(path)
if err != nil {
fmt.Println("Couldn't Read file: ", err)
}
return string(file)
}
func solve_part_one(data string) int {
pattern := `mul\((\d+),(\d+)\)`
regex := regexp.MustCompile(pattern)
// FindStringSubmatch returns the full match and all capture groups
matches := regex.FindAllStringSubmatch(data, -1)
ans := 0
// Iterate over all matches
for _, match := range matches {
// fmt.Printf("Match %d:\n", i+1)
// fmt.Println(" Full match:", match[0]) // Entire match
left, _ := strconv.Atoi(match[1])
right, _ := strconv.Atoi(match[2])
ans += left * right
}
return ans
}
func solve_part_two(data string) int {
pattern := `mul\((\d+),(\d+)\)|don't\(\)|do\(\)`
regex := regexp.MustCompile(pattern)
// FindStringSubmatch returns the full match and all capture groups
matches := regex.FindAllStringSubmatch(data, -1)
ans := 0
enabled := true
// Iterate over all matches
for _, match := range matches {
left, _ := strconv.Atoi(match[1])
right, _ := strconv.Atoi(match[2])
if match[0] == "don't()" {
enabled = false
} else if match[0] == "do()" {
enabled = true
}
if enabled {
ans += left * right
}
}
return ans
}
func main() {
test := FileRead("../input/day03.test")
test_2 := FileRead("../input/day03_2.test")
prod := FileRead("../input/day03.prod")
// Define the regex pattern with capture groups
fmt.Println("Part_1 test: ", solve_part_one(test))
fmt.Println("Part_1 prod: ", solve_part_one(prod))
fmt.Println("Part_2 test: ", solve_part_two(test_2))
fmt.Println("Part_2 prod: ", solve_part_two(prod))
}
|