-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
238 lines (202 loc) · 5.45 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main
import (
"fmt"
"strconv"
"strings"
aoc "github.com/shraddhaag/aoc/library"
)
func main() {
input := aoc.ReadFileLineByLine("input.txt")
fmt.Println("answer for part 1: ", getEngineNumbers(input))
fmt.Println("answer for part 2: ", getSlowEngineNumbers(input))
}
type engineNum struct {
start int
end int
num int64
}
func fetchNumsInInput(input []string) [][]engineNum {
numsInInput := [][]engineNum{}
for _, line := range input {
numsInLine := []engineNum{}
var build strings.Builder
var localNum engineNum
var err error
for j, char := range line {
if isDigit(byte(char)) {
build.WriteRune(char)
}
// if current character is not a digit and the previous character is a digit
// then it is a valid engine number and add it in sum
if !isDigit(byte(char)) && build.Len() != 0 {
// get the num
numS := build.String()
localNum.num, err = strconv.ParseInt(numS, 10, 12)
if err != nil {
panic(err)
}
localNum.end = j - 1
localNum.start = localNum.end - build.Len() + 1
build.Reset()
numsInLine = append(numsInLine, localNum)
localNum = engineNum{}
}
}
if build.Len() != 0 {
numS := build.String()
localNum.num, err = strconv.ParseInt(numS, 10, 64)
if err != nil {
panic(err)
}
localNum.end = len(line) - 1
localNum.start = localNum.end - build.Len() + 1
numsInLine = append(numsInLine, localNum)
}
numsInInput = append(numsInInput, numsInLine)
}
return numsInInput
}
func getEngineNumbers(input []string) int64 {
var sum int64
for i, value := range fetchNumsInInput(input) {
for _, localNum := range value {
// if char before number is symbol
if localNum.start != 0 && input[i][localNum.start-1] != '.' && !isDigit(input[i][localNum.start-1]) {
sum += localNum.num
continue
}
// if char after number is a symbol
if localNum.end < len(input[i])-1 && input[i][localNum.end+1] != '.' && !isDigit(input[i][localNum.end+1]) {
sum += localNum.num
continue
}
// if char in the line above or below have a symbol
k := 0
if localNum.start != 0 {
k = localNum.start - 1
}
end := localNum.end
if localNum.end < len(input[i])-1 {
end += 1
}
for ; k <= end; k++ {
if i != 0 {
if input[i-1][k] != '.' && !isDigit(input[i-1][k]) {
sum += localNum.num
break
}
}
if i != len(input)-1 {
if input[i+1][k] != '.' && !isDigit(input[i+1][k]) {
sum += localNum.num
break
}
}
}
}
}
return sum
}
func getSlowEngineNumbers(input []string) int64 {
var sum int64
numsInInput := fetchNumsInInput(input)
for i, line := range input {
for j, char := range line {
var count int
var mul int64
if char == '*' {
// has a digit before in the same line
if j != 0 && isDigit(line[j-1]) {
count++
for _, localNum := range numsInInput[i] {
if localNum.start <= j-1 && localNum.end >= j-1 {
if mul == 0 {
mul = localNum.num
} else {
mul *= localNum.num
}
break
}
}
}
// has a digit after in the same line
if j != len(line)-1 && isDigit(line[j+1]) {
count++
for _, localNum := range numsInInput[i] {
if localNum.start <= j+1 && localNum.end >= j+1 {
if mul == 0 {
mul = localNum.num
} else {
mul *= localNum.num
}
break
}
}
}
// has number/s in the line above or below
start := 0
if j != 0 {
start = j - 1
}
end := j
if j < len(line)-1 {
end += 1
}
// if line above has digit/s
if i != 0 {
for _, localNum := range numsInInput[i-1] {
// this is the tricky bit. Nums can exist in three different cases:
// note: '(' denotes beginning digit of number and ')' denotes the last digit
// note: '|' denote the start and beginning of the range where digit will be considered adjacent.
// 1. ----(-------)---- ie a single num spans the entire 3 'adjacent' indices
// ------|-*-|------
// 2. ----(---)-------- ie a num end in one of the allowed indices.
// ------|-*-|------
// 3. ---------(----)-- ie a num begins in one of the allowed indices.
// ------|-*-|------
// considering cases 2 & 3, we can have 2 numbers adjacent to a '*' in the lines above or below, eg:
// ---123-456----
// ------*-------
// case 1
if (localNum.start <= start && localNum.end >= end) ||
// case 2
(localNum.start <= start && localNum.end <= end && localNum.end >= start) ||
// case 3
(localNum.start >= start && localNum.start <= end && localNum.end >= end) {
count++
if mul == 0 {
mul = localNum.num
} else {
mul *= localNum.num
}
}
}
}
// if line below has digit/s
if i != len(input)-1 {
// same logic as above applied here, this time for lines below '*'
for _, localNum := range numsInInput[i+1] {
if (localNum.start <= start && localNum.end >= end) || (localNum.start <= start && localNum.end <= end && localNum.end >= start) || (localNum.start >= start && localNum.start <= end && localNum.end >= end) {
count++
if mul == 0 {
mul = localNum.num
} else {
mul *= localNum.num
}
}
}
}
}
if count == 2 {
sum += mul
}
}
}
return sum
}
func isDigit(char byte) bool {
if char >= 48 && char <= 57 {
return true
}
return false
}