-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.cpp
278 lines (232 loc) · 8.03 KB
/
day13.cpp
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using namespace std;
#include <iostream>
#include <string>
#include <utility>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <vector>
#include <cmath>
#include "Helpers/HelperFunctions.h"
queue<pair<int,int>*> getPairs(string str);
bool isCorrectValue(string &left, string trimmedLeft, queue<pair<int,int>*> &leftBrackets,
string &right, string trimmedRight, queue<pair<int,int>*> &rightBrackets);
bool hasDecided = false;
bool sortFunction(const string &lhs, const string &rhs);
int main() {
cout << "Day 13" << endl;
ifstream file(R"(C:\Users\gwen\Documents\2_Programming\Advent of Code\Advent of Code 2022\inputs\day13.txt)");
string str;
int index = 1;
int ans1 = 0;
string left = "";
string right = "";
queue<pair<int,int>*> leftBrackets;
queue<pair<int,int>*> rightBrackets;
vector<string> order;
while (getline(file, str)) {
if (str.empty()) {
// hasDecided = false;
// if (isCorrectValue(left, left, leftBrackets,
// right, right, rightBrackets)) {
// ans1 += index;
// cout << "True!" << endl;
// } else {
// cout << "False!" << endl;
// }
// left.clear();
// right.clear();
// queue<pair<int,int>*> empty = queue<pair<int,int>*>();
// swap(leftBrackets, empty);
// empty = queue<pair<int,int>*>();
// swap(rightBrackets, empty);
// index++;
// cout << "NEXT" << endl;
continue;
}
// queue<pair<int,int>*> *writeTo;
//
// if (left.empty()) {
// left = str;
// writeTo = &leftBrackets;
// } else {
// right = str;
// writeTo = &rightBrackets;
// }
order.push_back(str);
}
sort(order.begin(), order.end(), sortFunction);
cout << ans1 << endl;
// for (auto &it: order) {
// cout << it << endl;
// }
int ans2 = 1;
for (int i = 0; i < order.size(); i++) {
if (order[i] == "[[2]]" || order[i] == "[[6]]") {
ans2 *= i+1;
}
}
cout << ans2 << endl;
return 0;
}
bool sortFunction(const string &lhs, const string &rhs) {
queue<pair<int,int>*> leftBrackets = getPairs(lhs);
queue<pair<int,int>*> rightBrackets = getPairs(rhs);
hasDecided = false;
string left = lhs;
string right = rhs;
return isCorrectValue(left, left, leftBrackets, right, right, rightBrackets);
}
queue<pair<int,int>*> getPairs(string str) {
queue<pair<int,int>*> brackets;
stack<pair<int,int>*> positionPairs;
for (int i = 0; i < str.length(); i++) {
if (str[i] == '[') {
pair<int,int> *currPair = new pair<int,int>(i,0);
brackets.push(currPair);
positionPairs.push(currPair);
} else if (str[i] == ']') {
pair<int,int> *corresponding = positionPairs.top();
(*corresponding).second = i;
positionPairs.pop();
}
}
return brackets;
}
bool isCorrectValue(string &left, string trimmedLeft, queue<pair<int,int>*> &leftBrackets,
string &right, string trimmedRight, queue<pair<int,int>*> &rightBrackets) {
if (hasDecided) return true;
cout << "left: " << trimmedLeft << endl;
cout << "right: " << trimmedRight << endl;
if (trimmedLeft.empty() && trimmedRight.empty()) {
return true;
} else if (trimmedLeft.empty()) {
hasDecided = true;
return true;
}
if (trimmedRight.empty()) return false;
// four cases
if (trimmedLeft[0] == '[' && trimmedRight[0] == '[') {
string nextLeft;
pair<int,int> newLeftCurr;
if (!leftBrackets.empty()) {
newLeftCurr = *leftBrackets.front();
leftBrackets.pop();
nextLeft = left.substr(newLeftCurr.first+1, (newLeftCurr.second-newLeftCurr.first-1));
} else {
nextLeft = trimmedLeft.substr(1, trimmedLeft.length() - 2);
}
string nextRight;
pair<int,int> newRightCurr;
if (!rightBrackets.empty()) {
newRightCurr = *rightBrackets.front();
rightBrackets.pop();
nextRight = right.substr(newRightCurr.first+1, (newRightCurr.second-newRightCurr.first-1));
} else {
nextRight = trimmedRight.substr(1, trimmedRight.length() - 2);
}
if (isCorrectValue(left, nextLeft, leftBrackets, right, nextRight, rightBrackets)) {
string remainderLeft;
if (trimmedLeft.length() == nextLeft.length() + 2) {
remainderLeft = "";
} else {
remainderLeft = trimmedLeft.substr(nextLeft.length() + 3, -1);
}
string remainderRight;
if (trimmedRight.length() == nextRight.length() + 2) {
remainderRight = "";
} else {
remainderRight = trimmedRight.substr(nextRight.length() + 3, -1);
}
while (!leftBrackets.empty() && newLeftCurr.second > leftBrackets.front()->second) {
leftBrackets.pop();
}
while (!rightBrackets.empty() && newRightCurr.second > rightBrackets.front()->second) {
rightBrackets.pop();
}
return isCorrectValue(left, remainderLeft, leftBrackets, right, remainderRight, rightBrackets);
} else {
return false;
}
} else if (trimmedLeft[0] == '[') {
string nextLeft;
pair<int,int> newLeftCurr;
if (!leftBrackets.empty()) {
newLeftCurr = *leftBrackets.front();
leftBrackets.pop();
nextLeft = left.substr(newLeftCurr.first+1, (newLeftCurr.second-newLeftCurr.first-1));
} else {
nextLeft = trimmedLeft.substr(1, trimmedLeft.length() - 2);
}
size_t foundRight = trimmedRight.find(',');
string nextRight = trimmedRight.substr(0, foundRight);
if (isCorrectValue(left, nextLeft, leftBrackets, right, nextRight, rightBrackets)) {
string remainderLeft;
if (trimmedLeft.length() == nextLeft.length() + 2) {
remainderLeft = "";
} else {
remainderLeft = trimmedLeft.substr(nextLeft.length() + 3, -1);
}
string remainderRight;
if (trimmedRight.length() == nextRight.length()) {
remainderRight = "";
} else {
remainderRight = trimmedRight.substr(nextRight.length() + 1, -1);
}
while (!leftBrackets.empty() && newLeftCurr.second > leftBrackets.front()->second) {
leftBrackets.pop();
}
return isCorrectValue(left, remainderLeft, leftBrackets, right, remainderRight, rightBrackets);
} else {
return false;
}
} else if (trimmedRight[0] == '[') {
size_t foundLeft = trimmedLeft.find(',');
string nextLeft = trimmedLeft.substr(0, foundLeft);
string nextRight;
pair<int,int> newRightCurr;
if (!rightBrackets.empty()) {
newRightCurr = *rightBrackets.front();
rightBrackets.pop();
nextRight = right.substr(newRightCurr.first+1, (newRightCurr.second-newRightCurr.first-1));
} else {
nextRight = trimmedRight.substr(1, trimmedRight.length() - 2);
}
if (isCorrectValue(left, nextLeft, leftBrackets, right, nextRight, rightBrackets)) {
string remainderLeft;
if (trimmedLeft.length() == nextLeft.length()) {
remainderLeft = "";
} else {
remainderLeft = trimmedLeft.substr(nextLeft.length() + 1, -1);
}
string remainderRight;
if (trimmedRight.length() == nextRight.length() + 2) {
remainderRight = "";
} else {
remainderRight = trimmedRight.substr(nextRight.length() + 3, -1);
}
while (!rightBrackets.empty() && newRightCurr.second > rightBrackets.front()->second) {
rightBrackets.pop();
}
return isCorrectValue(left, remainderLeft, leftBrackets, right, remainderRight, rightBrackets);
} else {
return false;
}
} else {
size_t foundLeft = trimmedLeft.find(',');
size_t foundRight = trimmedRight.find(',');
int valLeft = stoi(trimmedLeft.substr(0, foundLeft));
int valRight = stoi(trimmedRight.substr(0, foundRight));
if (valLeft < valRight) {
hasDecided = true;
return true;
} else if (valLeft == valRight) {
string nextLeft = (foundLeft != string::npos) ? trimmedLeft.substr(foundLeft + 1) : "";
string nextRight = (foundRight != string::npos) ? trimmedRight.substr(foundRight + 1) : "";
return isCorrectValue(left, nextLeft, leftBrackets, right, nextRight, rightBrackets);
} else {
return false;
}
}
}