-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay03.java
254 lines (182 loc) · 7.23 KB
/
Day03.java
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Day03 extends AocSolver {
protected Day03(String filename) {
super(filename);
}
public static void main(String[] args) {
new Day03("resources/day_03.txt");
}
@Override
protected String runPart1(List<String> lines) {
int total = 0;
for (int lineNumber = 0; lineNumber < lines.size(); lineNumber++) {
String line = lines.get(lineNumber);
Pattern pattern = Pattern.compile("(\\d+)");
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
String number = matcher.group(1);
int startIndex = matcher.start();
int endIndex = matcher.end();
boolean isPartNumber = false;
// check left
if (startIndex > 0) { // check for out of bounds
char charBefore = line.charAt(startIndex - 1);
if (isSymbol(charBefore)) {
isPartNumber = true;
}
}
// check right
if (endIndex < line.length()) { // check for out of bounds
char charAfter = line.charAt(endIndex);
if (isSymbol(charAfter)) {
isPartNumber = true;
}
}
// check above
if (lineNumber > 0) { // not on first line
// clamp from beginning and end
int aboveStart = startIndex - 1;
if (aboveStart < 0) {
aboveStart = 0;
}
int aboveEnd = endIndex + 1;
if (aboveEnd >= line.length()) {
aboveEnd = line.length() - 1;
}
String substring = lines.get(lineNumber - 1).substring(aboveStart, aboveEnd);
if (containsSymbol(substring)) {
isPartNumber = true;
}
}
// check bellow
if (lineNumber != lines.size() - 1) { // not on last line
// clamp from beginning and end
int aboveStart = startIndex - 1;
if (aboveStart < 0) {
aboveStart = 0;
}
int aboveEnd = endIndex + 1;
if (aboveEnd >= line.length()) {
aboveEnd = line.length() - 1;
}
String substring = lines.get(lineNumber + 1).substring(aboveStart, aboveEnd);
if (containsSymbol(substring)) {
isPartNumber = true;
}
}
if (isPartNumber) {
total += Integer.parseInt(number);
}
}
}
return Integer.toString(total);
}
@Override
protected String runPart2(List<String> lines) {
int total = 0;
for (int lineNumber = 0; lineNumber < lines.size(); lineNumber++) {
String line = lines.get(lineNumber);
Pattern pattern = Pattern.compile("\\*");
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
int gearIndex = matcher.start();
List<Integer> gearNumbers = new ArrayList<>();
// check left
if (gearIndex > 0) { // check for out of bounds
int numberBefore = findNumberBefore(gearIndex, line);
if (numberBefore > 0) {
gearNumbers.add(numberBefore);
}
}
// check right
if (gearIndex < line.length()) { // check for out of bounds
int numberAfter = findNumberAfter(gearIndex, line);
if (numberAfter > 0) {
gearNumbers.add(numberAfter);
}
}
// check above
if (lineNumber > 0) { // not on first line
List<Integer> numbersAbove = findNumbersBeforeAfter(gearIndex, lines.get(lineNumber - 1));
if (!numbersAbove.isEmpty()) {
gearNumbers.addAll(numbersAbove);
}
}
// check bellow
if (lineNumber != lines.size() - 1) { // not on last line
List<Integer> numbersBellow = findNumbersBeforeAfter(gearIndex, lines.get(lineNumber + 1));
if (!numbersBellow.isEmpty()) {
gearNumbers.addAll(numbersBellow);
}
}
if (gearNumbers.size() == 2) {
total += gearNumbers.get(0) * gearNumbers.get(1);
}
}
}
return Integer.toString(total);
}
private boolean isSymbol(char symbol) {
return symbol != '.';
}
private boolean containsSymbol(String str) {
for (char c : str.toCharArray()) {
if (isSymbol(c)) {
return true;
}
}
return false;
}
private List<Integer> findNumbersBeforeAfter(int charIndex, String line) {
List<Integer> numbers = new ArrayList<>();
if (Character.isDigit(line.charAt(charIndex))) {
// there is only 1 number above - find it
int numberAbove = findNumberAtIndex(charIndex, line);
numbers.add(numberAbove);
} else {
// there are 0-2 numbers above - check before and after
int numberBefore = findNumberBefore(charIndex, line);
int numberAfter = findNumberAfter(charIndex, line);
if (numberBefore != 0) {
numbers.add(numberBefore);
}
if (numberAfter != 0) {
numbers.add(numberAfter);
}
}
return numbers;
}
private int findNumberAtIndex(int charIndex, String line) {
// we find the beginning of the number
while (Character.isDigit(line.charAt(charIndex - 1))) {
charIndex--;
}
String numberStr = "";
while (Character.isDigit(line.charAt(charIndex))) {
numberStr += line.charAt(charIndex);
charIndex++;
}
return Integer.parseInt(numberStr);
}
private int findNumberBefore(int charIndex, String line) {
String substring = line.substring(0, charIndex);
Pattern pattern = Pattern.compile(".*?(\\d+)$");
Matcher matcher = pattern.matcher(substring);
if (matcher.find()) {
return Integer.parseInt(matcher.group(1));
}
return 0;
}
private int findNumberAfter(int charIndex, String line) {
String substring = line.substring(charIndex + 1);
Pattern patternLeft = Pattern.compile("^(\\d+).*");
Matcher matcherLeft = patternLeft.matcher(substring);
if (matcherLeft.find()) {
return Integer.parseInt(matcherLeft.group(1));
}
return 0;
}
}