-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyntaxScorer.java
More file actions
212 lines (204 loc) · 6.95 KB
/
SyntaxScorer.java
File metadata and controls
212 lines (204 loc) · 6.95 KB
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;
/**
* Solution to Day 10: Syntax Scoring.
*
* @author bluebillxp
*/
public class SyntaxScorer {
public static void main(String[] args) {
List<String> input = AdventHelper.readInput("input-day10-syntax-scoring.txt");
System.out.println("Day 10: Syntax Scoring " + input.size() + " commands loaded.");
System.out.println("Day 10: Syntax Scoring --- Part One ---");
System.out.println("What is the total syntax error score for those errors?");
final int answerOne = solutionPartOne(input);
System.out.println("Answer: " + answerOne);
System.out.println("\nDay 10: Syntax Scoring --- Part Two ---");
System.out.println("Find the completion string for each incomplete line, score the completion strings, and sort the scores. What is the middle score?");
final long answerTwo = solutionPartTwo(input);
System.out.println("Answer: " + answerTwo);
}
/**
* Calculates the total syntax error score for those errors.
*
* @param input Defined input from the challenge.
*
* @return answer
*/
private static int solutionPartOne(List<String> input) {
int[] corruptedChars = new int[4];
for (String syntaxLine : input) {
char incorrectChar = findFirstIncorrectChar(syntaxLine);
switch (incorrectChar) {
case ')': {
corruptedChars[0]++;
break;
}
case ']': {
corruptedChars[1]++;
break;
}
case '}': {
corruptedChars[2]++;
break;
}
case '>': {
corruptedChars[3]++;
break;
}
}
}
int totalScore = 0;
totalScore += (corruptedChars[0] * 3);
totalScore += (corruptedChars[1] * 57);
totalScore += (corruptedChars[2] * 1197);
totalScore += (corruptedChars[3] * 25137);
return totalScore;
}
/**
* Calculates the middle score of incomplete lines.
*
* @param input Defined input from the challenge.
*
* @return answer
*/
private static long solutionPartTwo(List<String> input) {
ArrayList<Long> scores = new ArrayList<>();
for (String line : input) {
long score = computeAutocompleteScore(line);
if (score > 0) {
scores.add(score);
}
}
Collections.sort(scores);
return scores.get(scores.size()/2);
}
private static char findFirstIncorrectChar(String line) {
// Whatever pops is not a pair, then it's corrupted.
// Whatever pops is a pair, but not empty the stack, it's incomplete.
// Wherever pops is a pait, and empty the stack, it's complete.
Stack<Character> syntaxPairStack = new Stack<>();
boolean corrupted = false;
char corruptedChar = '?';
for (int i = 0; i < line.length(); i++) {
char ch = line.charAt(i);
switch (ch) {
case ')': {
Character pairCh = syntaxPairStack.pop();
if (pairCh.charValue() != '(') {
corrupted = true;
}
break;
}
case ']': {
Character pairCh = syntaxPairStack.pop();
if (pairCh.charValue() != '[') {
corrupted = true;
}
break;
}
case '}': {
Character pairCh = syntaxPairStack.pop();
if (pairCh.charValue() != '{') {
corrupted = true;
}
break;
}
case '>': {
Character pairCh = syntaxPairStack.pop();
if (pairCh.charValue() != '<') {
corrupted = true;
}
break;
}
default: {
syntaxPairStack.push(Character.valueOf(ch));
break;
}
}
if (corrupted) {
corruptedChar = ch;
break;
}
}
return corruptedChar;
}
private static long computeAutocompleteScore(String line) {
Stack<Character> syntaxPairStack = new Stack<>();
boolean corrupted = false;
for (int i = 0; i < line.length(); i++) {
char ch = line.charAt(i);
switch (ch) {
case ')': {
Character pairCh = syntaxPairStack.pop();
if (pairCh.charValue() != '(') {
corrupted = true;
}
break;
}
case ']': {
Character pairCh = syntaxPairStack.pop();
if (pairCh.charValue() != '[') {
corrupted = true;
}
break;
}
case '}': {
Character pairCh = syntaxPairStack.pop();
if (pairCh.charValue() != '{') {
corrupted = true;
}
break;
}
case '>': {
Character pairCh = syntaxPairStack.pop();
if (pairCh.charValue() != '<') {
corrupted = true;
}
break;
}
default: {
syntaxPairStack.push(Character.valueOf(ch));
break;
}
}
if (corrupted) {
break;
}
}
if (corrupted) {
return 0;
}
long autocompleteScore = 0;
while (syntaxPairStack.size() > 0) {
Character leadingCh = syntaxPairStack.pop();
autocompleteScore *= 5;
switch (leadingCh.charValue()) {
case '(': {
autocompleteScore += 1;
System.out.print(')');
break;
}
case '[': {
autocompleteScore += 2;
System.out.print(']');
break;
}
case '{': {
autocompleteScore += 3;
System.out.print('}');
break;
}
case '<': {
autocompleteScore += 4;
System.out.print('>');
break;
}
}
}
System.out.println(autocompleteScore);
return autocompleteScore;
}
}