-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
217 lines (190 loc) · 4.56 KB
/
app.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"sort"
"strconv"
"strings"
)
// math.Abs is float
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
var total = 0
func main() {
day1()
day2(false)
day2(true)
day3()
day3Part2()
}
func day1() {
total := 0
left := []int{}
right := []int{}
// Read the file
file, err := os.Open("day1.txt")
if err != nil {
panic(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
parts := strings.Fields(line)
if len(parts) == 2 {
l, err1 := strconv.Atoi(parts[0])
r, err2 := strconv.Atoi(parts[1])
if err1 != nil || err2 != nil {
panic("Invalid number in input")
}
left = append(left, l)
right = append(right, r)
}
}
if err := scanner.Err(); err != nil {
panic(err)
}
// Sort the slices
sort.Ints(left)
sort.Ints(right)
// Calculate the total difference
for i := 0; i < len(left); i++ {
total += abs(right[i] - left[i])
}
fmt.Println("day 1:", total)
}
func day2(part2 bool) {
total := 0
// Read the file
file, err := os.Open("day2.txt")
if err != nil {
panic(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
parts := strings.Fields(line)
ary := make([]int, len(parts))
for i := range ary {
ary[i], _ = strconv.Atoi(parts[i])
}
if isIncreasingOrDecreasing(ary) {
total += 1
} else {
if part2 {
for i := 0; i < len(ary); i++ {
// Create a copy of the array excluding the i-th element
newArray := append([]int{}, ary[:i]...) // Copy elements before i
newArray = append(newArray, ary[i+1:]...) // Copy elements after i
if isIncreasingOrDecreasing(newArray) {
fmt.Println("Problem Dampener success")
total += 1
break
}
}
}
}
}
if err := scanner.Err(); err != nil {
panic(err)
}
if !part2 {
fmt.Println("day2 part 1:", total)
} else {
fmt.Println("day2 part 2:", total)
}
}
func day3() {
// Compile the regex to match `mul(number,number)`
regex := regexp.MustCompile(`mul\((\d+),(\d+)\)`)
// Read the entire file into memory
content, err := os.ReadFile("day3.txt")
if err != nil {
fmt.Println("Error reading file:", err)
return
}
// Convert the file content to a string
text := string(content)
var total int
// Find all matches in the text
matches := regex.FindAllStringSubmatch(text, -1)
for _, match := range matches {
if len(match) == 3 { // Ensure we have two capture groups
// Parse the numbers from the match
num1, err1 := strconv.Atoi(match[1])
num2, err2 := strconv.Atoi(match[2])
if err1 == nil && err2 == nil {
total += num1 * num2
} else {
fmt.Println("Error parsing numbers:", err1, err2)
}
}
}
fmt.Println("day3 part1:", total)
}
func day3Part2() {
// Regular expressions to match `mul`, `do()`, and `don't()` instructions
mulRegex := regexp.MustCompile(`mul\((\d+),(\d+)\)`)
doRegex := regexp.MustCompile(`do\(\)`)
dontRegex := regexp.MustCompile(`don't\(\)`)
// Read the entire file into memory
content, err := os.ReadFile("day3.txt")
if err != nil {
fmt.Println("Error reading file:", err)
return
}
// Convert the file content to a string
text := string(content)
var total int
mulEnabled := true // mul instructions are initially enabled
// Process each character to extract meaningful instructions
for _, line := range regexp.MustCompile(`(mul\(\d+,\d+\)|do\(\)|don't\(\))`).FindAllString(text, -1) {
// Handle enabling and disabling
if doRegex.MatchString(line) {
mulEnabled = true
} else if dontRegex.MatchString(line) {
mulEnabled = false
} else if mulEnabled && mulRegex.MatchString(line) {
// Process `mul` instructions only if they are enabled
match := mulRegex.FindStringSubmatch(line)
if len(match) == 3 {
// Parse the numbers from the match
num1, err1 := strconv.Atoi(match[1])
num2, err2 := strconv.Atoi(match[2])
if err1 == nil && err2 == nil {
total += num1 * num2
} else {
fmt.Println("Error parsing numbers:", err1, err2)
}
}
}
}
fmt.Println("day3 part2:", total)
}
func isIncreasingOrDecreasing(nums []int) bool {
isIncreasing := true
isDecreasing := true
for i := 1; i < len(nums); i++ {
diff := nums[i] - nums[i-1]
// Check if the difference is outside the allowed range
if diff < -3 || diff > 3 {
return false
}
// Check if the sequence is not strictly increasing
if diff <= 0 {
isIncreasing = false
}
// Check if the sequence is not strictly decreasing
if diff >= 0 {
isDecreasing = false
}
}
return isIncreasing || isDecreasing
}