-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart_2.go
53 lines (47 loc) · 1 KB
/
part_2.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
51
52
53
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
)
func resultOfEnabledMultiply() uint32 {
// Open the input file
file, err := os.Open("./input.txt")
// fmt.Println(file)
if err != nil {
fmt.Println("Error opening file:", err)
return 0
}
defer file.Close()
// Read the file content
scanner := bufio.NewScanner(file)
var corruptedMemory string
for scanner.Scan() {
corruptedMemory += scanner.Text()
}
// Compile the regex pattern
re := regexp.MustCompile(`mul\(([0-9]{1,3}),([0-9]{1,3})\)|don't\(\)|do\(\)`)
// Set the enabled flag to true
enabled := true
total := uint32(0)
// Iterate over the regex matches
matches := re.FindAllStringSubmatch(corruptedMemory, -1)
for _, match := range matches {
switch match[0] {
case "do()":
enabled = true
case "don't()":
enabled = false
default:
if enabled {
// Parse the numbers and multiply them
x, _ := strconv.Atoi(match[1])
y, _ := strconv.Atoi(match[2])
total += uint32(x * y)
}
}
}
return total
}