-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
66 lines (52 loc) · 1.15 KB
/
main.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
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"regexp"
"github.com/Alex-Waring/AoC/utils"
)
func part1(input []string) {
defer utils.Timer("part1")()
total := 0
for _, line := range input {
r := regexp.MustCompile(`mul\(\d*,\d*\)`)
matches := r.FindAllString(line, -1)
for _, match := range matches {
num_r := regexp.MustCompile(`\d+`)
nums := num_r.FindAllString(match, -1)
total += utils.IntegerOf(nums[0]) * utils.IntegerOf(nums[1])
}
}
fmt.Println(total)
}
func part2(input []string) {
defer utils.Timer("part2")()
total := 0
matches := []string{}
r := regexp.MustCompile(`mul\(\d*,\d*\)|do\(\)|don't\(\)`)
for _, line := range input {
matches = append(matches, r.FindAllString(line, -1)...)
}
enabled := true
for _, match := range matches {
if match == "do()" {
enabled = true
continue
}
if match == "don't()" {
enabled = false
continue
}
if !enabled {
continue
}
num_r := regexp.MustCompile(`\d+`)
nums := num_r.FindAllString(match, -1)
total += utils.IntegerOf(nums[0]) * utils.IntegerOf(nums[1])
}
fmt.Println(total)
}
func main() {
input := utils.ReadInput("input.txt")
part1(input)
part2(input)
}