-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdayTwoPartTwo.cpp
More file actions
322 lines (276 loc) · 11.5 KB
/
dayTwoPartTwo.cpp
File metadata and controls
322 lines (276 loc) · 11.5 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
// Below this code is an attempt at completing this challenge in a single read of the line without array-like structures.
// It only seemed to work for popping n elements, not n+1 or n-1.
namespace DayTwoPartTwo
{
namespace {
using std::string; // This program is not advanced enough to use non-standard strings. I won't use using namespace std
const bool toLog = false;
const int kMinDiff = 1; // google's constant naming convention is kPascalCase
const int kMaxDiff = 3;
const char kDelim = ' ';
bool twoLevelsAreSafe(const int parsedEntry, const int previousLevel, const bool comparisonIsLargerThan)
{
int diff = parsedEntry - previousLevel;
if (abs(diff) < kMinDiff || abs(diff) > kMaxDiff)
{
if (toLog) std::cout << parsedEntry << '-' << previousLevel << ": F Both distance\n";
return false;
}
if (toLog) std::cout << "Comparison: " << parsedEntry << '-' << previousLevel << " for: " << comparisonIsLargerThan << "\n";
if (comparisonIsLargerThan)
{
if (diff < 0) {
if (toLog) std::cout << parsedEntry << '-' << previousLevel << ": F Both comparison < 0\n";
return false;
};
}
else
{
if (diff > 0) {
if (toLog) std::cout << parsedEntry << '-' << previousLevel << ": F Both comparison > 0\n";
return false;
}
}
if (toLog) std::cout << parsedEntry << "-" << previousLevel << ": Is Valid\n";
return true;
}
bool isDampenedRunValid(std::vector<int> entriesCopy, size_t i)
{
if (toLog) std::cout << "__Damper run for index: " << i << "\n";
if (toLog) std::cout << "__ index: " << i << "has value: " << entriesCopy[i] << "\n";
entriesCopy.erase(entriesCopy.begin() + (i));
size_t size = entriesCopy.size();
bool comparisonIsLargerThan = entriesCopy[1] - entriesCopy[0] > 0;
for (size_t i = 1; i < size; ++i) {
if (!twoLevelsAreSafe(entriesCopy[i], entriesCopy[i - 1], comparisonIsLargerThan)) return false;
}
return true;
}
bool isAnyDampenedRunValid(const std::vector<int> entries, size_t i)
{
if (entries.size() < 4) throw std::runtime_error::runtime_error("Test set contains invalid entries");
if (i == 2) if (isDampenedRunValid(entries, 0)) return true;
if (isDampenedRunValid(entries, i - 1)) return true;
if (isDampenedRunValid(entries, i)) return true;
if (i + 1 < entries.size())
{
if (isDampenedRunValid(entries, i + 1)) return true;
}
return false;
}
bool handleLine(const std::string& line) { // actual source value is passed but cannot be edited
if (toLog) std::cout << "----\n>" << line << "\n";
std::vector<int> entries;
bool potentialInvalidEntryHasBeenFound = false;
size_t i = 1;
std::stringstream ss(line); // every getline removes that entryToParse based on the delim
bool comparisonIsLargerThan; // default value is false but not meant to be utilized pre-explicit definition
string entryToParse;
if (getline(ss, entryToParse, kDelim)) {
int previousLevel = std::stoi(entryToParse);
entries.push_back(previousLevel);
if (getline(ss, entryToParse, kDelim))
{
int secondEntry = std::stoi(entryToParse);
entries.push_back(secondEntry);
int diff = secondEntry - previousLevel;
comparisonIsLargerThan = diff > 0;
if (abs(diff) < kMinDiff || abs(diff) > kMaxDiff)
{
if (toLog) std::cout << "Second entry invalid\n";
potentialInvalidEntryHasBeenFound = true;
}
else if (toLog) std::cout << "Second entry is valid\n";
previousLevel = secondEntry;
}
else throw std::runtime_error::runtime_error("Failed getline parsing");
while (getline(ss, entryToParse, kDelim)) {
int parsedEntry = std::stoi(entryToParse);
entries.push_back(parsedEntry);
if (!potentialInvalidEntryHasBeenFound) i++;
if (!twoLevelsAreSafe(parsedEntry, previousLevel, comparisonIsLargerThan)) potentialInvalidEntryHasBeenFound = true;
previousLevel = parsedEntry;
}
if (!potentialInvalidEntryHasBeenFound) return true;
if (isAnyDampenedRunValid(entries, i)) return true;
return false;
}
else throw std::runtime_error::runtime_error("Failed getline parsing"); // Program should not be fail - tolerant
return true;
}
}
void dayTwoPartTwo() {
std::cout << "Running program dayTwoPartTwo" << '\n';
std::string line;
std::ifstream inputFile("dayTwoFull.txt");
if (inputFile.is_open())
{
int safeLines = 0;
while (getline(inputFile, line)) {
if (handleLine(line)) {
safeLines++;
//std::cout << "safe entry " << safeLines << ": " << line << "\n";
}
else std::cout << line << '\n';
}
std::cout << "Safe lines:" << safeLines << '\n';
std::cout << "done reading file\n";
}
else std::cout << "Unable to open file";
}
// dayTwoTest
// dayTwoFull
// Below code was for a self-imposed challenge: no double runthrough.
// Easier algo above has multiple runs per line: one to find the wrong one, pop it, then run through the full line and check for validity.
// Does not accomodate n+1 and n-1 checking, only popping element n.
//namespace {
// using std::string;
// const bool toLog = true;
// const int kMinDiff = 1; // google's constant naming convention is kPascalCase
// const int kMaxDiff = 3;
// const char kDelim = ' ';
// void updateVariablesForValidLevels(const int parsedEntry, int& previousLevel, int& secondLastLevel)
// {
// secondLastLevel = previousLevel;
// previousLevel = parsedEntry;
// if (toLog) std::cout << "NormlUpdate: S" << secondLastLevel << "|P" << previousLevel << "|\n\n";
// }
// bool twoLevelsAreSafe(const int parsedEntry, const int previousLevel)
// {
// int diff = parsedEntry - previousLevel;
// if (abs(diff) < kMinDiff || abs(diff) > kMaxDiff)
// {
// if (toLog) std::cout << parsedEntry << '-' << previousLevel << ": F Only distance\n";
// return false;
// }
// return true;
// }
// bool doesLoopHaveToContinue(bool* hasDampenerBeenUsed, int& parsedEntryToUndo, const int previousLevel) {
// if (hasDampenerBeenUsed == nullptr) throw std::runtime_error::runtime_error("hasDampenerBeenUsed NullpointerException");
// if (!*hasDampenerBeenUsed)
// {
// if (toLog) std::cout << "Used up dampener, " << parsedEntryToUndo << " is now " << previousLevel << "\n";
// parsedEntryToUndo = previousLevel;
// *hasDampenerBeenUsed = true;
// return true;
// }
// if (toLog) std::cout << "No dampener available\n";
// return false;
// }
// bool twoLevelsAreSafe(int& parsedEntry, const int previousLevel, const bool* comparisonIsLargerThan, bool* hasDampenerBeenUsed)
// {
// if (toLog) std::cout << "AreSafe? " << "P" << previousLevel << "|E" << parsedEntry << "|\n";
// int diff = parsedEntry - previousLevel;
// if (abs(diff) < kMinDiff || abs(diff) > kMaxDiff)
// {
// if (toLog) std::cout << parsedEntry << '-' << previousLevel << ": F Both distance\n";
// return doesLoopHaveToContinue(hasDampenerBeenUsed, parsedEntry, previousLevel);
// }
// if (comparisonIsLargerThan != nullptr)
// {
// if (toLog) std::cout << "Comparison: " << parsedEntry << '-' << previousLevel << " for: " << *comparisonIsLargerThan << "\n";
// if (*comparisonIsLargerThan)
// {
// if (diff < 0) {
// if (toLog) std::cout << parsedEntry << '-' << previousLevel << ": F Both comparison < 0\n";
// return doesLoopHaveToContinue(hasDampenerBeenUsed, parsedEntry, previousLevel);
// };
// }
// else
// {
// if (diff > 0) {
// if (toLog) std::cout << parsedEntry << '-' << previousLevel << ": F Both comparison > 0\n";
// return doesLoopHaveToContinue(hasDampenerBeenUsed, parsedEntry, previousLevel);
// }
// }
// }
// else throw std::runtime_error::runtime_error("comparisonIsLargerThan NullpointerException");
// return true;
// }
// void updateVariablesForValidFirstLevels(bool& comparisonIsLargerThan, const int parsedEntry, int& previousLevel, int& secondLastLevel)
// {
// comparisonIsLargerThan = (parsedEntry - previousLevel) > 0;
// secondLastLevel = previousLevel;
// previousLevel = parsedEntry;
// if (toLog) std::cout << "FirstUpdate: S" << secondLastLevel << "|P" << previousLevel << "|E" << parsedEntry << "\n";
// }
// /// <summary>
// /// Pre check before full loop can go
// /// </summary>
// bool areFirstEntriesValid(std::stringstream& ss, std::string& entryToParse, int& previousLevel, int& secondLastLevel,
// bool& comparisonIsLargerThan, bool& hasDampenerBeenUsed)
// {
// if (getline(ss, entryToParse, kDelim)) // first check with entry 1 & 2
// {
// int parsedEntry = std::stoi(entryToParse);
// if (twoLevelsAreSafe(previousLevel, parsedEntry)) {
// updateVariablesForValidFirstLevels(comparisonIsLargerThan, parsedEntry, previousLevel, secondLastLevel);
// return true;
// }
// else if (getline(ss, entryToParse, kDelim)) {
// hasDampenerBeenUsed = true;
// if (toLog) std::cout << "Second entry invalid\n";
// secondLastLevel = previousLevel;
// parsedEntry = std::stoi(entryToParse);
// if (toLog) std::cout << "&&S" << secondLastLevel << "|P" << previousLevel << "|E" << parsedEntry << "|\n";
// if (twoLevelsAreSafe(secondLastLevel, parsedEntry)) { //entry 1 & 3
// comparisonIsLargerThan = (parsedEntry - previousLevel) > 0;
// return true;
// }
// else {
// if (toLog) std::cout << "Second & Third entry invalid\n";
// return false;
// }
// }
// else throw std::runtime_error("Failed getline parsing");
// }
// else throw std::runtime_error::runtime_error("Failed getline parsing"); // Program should not be fail-tolerant
// }
// bool handleLine(const std::string& line) { // actual source value is passed but cannot be edited
// if (toLog) std::cout << "----\n>" << line << "\n";
// std::stringstream ss(line);
// bool comparisonIsLargerThan; // default value is false but not meant to be utilized pre-explicit definition
// bool hasDampenerBeenUsed = false;
// int secondLastLevel = 0;
// string entryToParse;
// if (getline(ss, entryToParse, kDelim)) { // every getline removes that entryToParse based on the delim
// int previousLevel = std::stoi(entryToParse);
// if (!areFirstEntriesValid(ss, entryToParse, previousLevel, secondLastLevel, comparisonIsLargerThan, hasDampenerBeenUsed)) return false;
// while (getline(ss, entryToParse, kDelim)) {
// int parsedEntry = std::stoi(entryToParse);
// if (!twoLevelsAreSafe(parsedEntry, previousLevel, &comparisonIsLargerThan, &hasDampenerBeenUsed)) return false;
// if (toLog) std::cout << "Pre update: S" << secondLastLevel << "|P" << previousLevel << "|E" << parsedEntry << "\n";
// updateVariablesForValidLevels(parsedEntry, previousLevel, secondLastLevel);
// }
// }
// else throw std::runtime_error::runtime_error("Failed getline parsing"); // Program should not be fail-tolerant
// return true;
// }
//}
//void dayTwoPartTwo() {
// std::cout << "Running program dayTwoPartTwo" << '\n';
// std::string line;
// std::ifstream inputFile("dayTwoTest.txt");
// if (inputFile.is_open())
// {
// int safeLines = 0;
// while (getline(inputFile, line)) {
// if (handleLine(line)) {
// safeLines++;
// if (toLog) std::cout << "safe entry " << safeLines << ": " << line << "\n";
// }
// else std::cout << "unsafe: " << line << '\n';
// }
// std::cout << "Safe lines:" << safeLines << '\n';
// std::cout << "done reading file\n";
// }
// else std::cout << "Unable to open file";
//}
}
// dayTwoTest
// dayTwoFull