-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday03
More file actions
65 lines (51 loc) · 1.25 KB
/
day03
File metadata and controls
65 lines (51 loc) · 1.25 KB
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
package main
import (
"fmt"
"regexp"
"strconv"
)
func main() {
var lines = GetInputData("./inputs/day03")
Day03Part1(lines)
Day03Part2(lines)
}
func CalculateProduct(match []string) int {
var a, _ = strconv.Atoi(match[1])
var b, _ = strconv.Atoi(match[2])
var product = a * b
return product
}
func Day03Part1(lines []string) {
var r, _ = regexp.Compile(`mul\(([0-9]{1,3}),([0-9]{1,3})\)`)
var result = 0
for _, line := range lines {
var matches = r.FindAllStringSubmatch(line, -1)
// fmt.Println(matches)
for i := 0; i < len(matches); i++ {
var match = matches[i]
var product = CalculateProduct(match)
result = result + product
}
}
fmt.Println("Answer Day03 Part 1: ", result)
}
func Day03Part2(lines []string) {
var r, _ = regexp.Compile(`do\(\)|don't\(\)|mul\(([0-9]{1,3}),([0-9]{1,3})\)`)
var result = 0
var isEnabled = true
for _, line := range lines {
var matches = r.FindAllStringSubmatch(line, -1)
for i := 0; i < len(matches); i++ {
var match = matches[i]
if match[0] == "do()" {
isEnabled = true
} else if match[0] == "don't()" {
isEnabled = false
} else if isEnabled {
var product = CalculateProduct(match)
result = result + product
}
}
}
fmt.Println("Answer Day03 Part 2: ", result)
}