-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.go
96 lines (78 loc) · 2.03 KB
/
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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"bufio"
"os"
"regexp"
"strconv"
"time"
)
func day3() {
println("Solving AoC 2024 Day 3:")
lines := loadInputDay3()
day3part1(lines)
day3part2(lines)
}
func loadInputDay3() []string {
defer timeTrack(time.Now(), "Loading/Parsing Input")
file, err := os.Open("inputs/day3.txt")
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(file)
lines := make([]string, 0)
for scanner.Scan() {
var line = scanner.Text()
lines = append(lines, line)
}
return lines
}
func day3part1(lines []string) {
defer timeTrack(time.Now(), "Part1")
var mulPattern = regexp.MustCompile("(mul\\(\\d{1,3},\\d{1,3}\\))")
var sum = 0
for _, line := range lines {
mulExpressions := mulPattern.FindAllString(line, -1)
for _, expression := range mulExpressions {
var numberPattern = regexp.MustCompile("(\\d{1,3})")
matchedNumbers := numberPattern.FindAllString(expression, -1)
if len(matchedNumbers) != 2 {
panic("Matched Mul did not include 2 numbers")
}
i, _ := strconv.Atoi(matchedNumbers[0])
j, _ := strconv.Atoi(matchedNumbers[1])
sum += i * j
}
}
println(sum)
}
func day3part2(lines []string) {
defer timeTrack(time.Now(), "Part2")
//var line = "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))"
var matchPattern = regexp.MustCompile("(?:mul\\(\\d{1,3},\\d{1,3}\\))|(?:don't\\(\\))|(?:do\\(\\))")
var sum = 0
var enabled = true
for _, line := range lines {
matchExpressions := matchPattern.FindAllString(line, -1)
for _, expression := range matchExpressions {
if expression == "do()" {
enabled = true
continue
} else if expression == "don't()" {
enabled = false
continue
}
if !enabled {
continue
}
var numberPattern = regexp.MustCompile("(\\d{1,3})")
matchedNumbers := numberPattern.FindAllString(expression, -1)
if len(matchedNumbers) != 2 {
panic("Matched Mul did not include 2 numbers")
}
i, _ := strconv.Atoi(matchedNumbers[0])
j, _ := strconv.Atoi(matchedNumbers[1])
sum += i * j
}
}
println(sum)
}