-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdayTwelvePartTwo.cpp
More file actions
421 lines (356 loc) · 16 KB
/
dayTwelvePartTwo.cpp
File metadata and controls
421 lines (356 loc) · 16 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <set> // no default pair hashing in c++14
#include <map>
namespace DayTwelvePartTwo // The most difficult one so far.
{
namespace {
using square_vect_t = std::vector<std::vector<char>>;
using coord_t = std::pair<int, int>;
enum class Direction { Up, Right, Down, Left, None, Invalid };
bool isCoordWithinBounds(const coord_t& nextCoord, const int kSquareLen);
bool hasCharAtCoord(const square_vect_t& charSquare, const char prevChar, const coord_t& coordToCheck, const int kSquareLen);
void fillLineInVector(const std::string line, square_vect_t& charSquare, const int kSquareLen, const int currentRow);
void coutCharSquareWithChecked(const square_vect_t& charSquare, const int kSquareLen);
void coutRegisteredBordersOverlay(const std::set<coord_t>& registeredBorders, const int kSquareLen, const std::set<coord_t>& priorityCoords, const std::set<coord_t>& otherCoords);
void coutSetContents(const std::string prefix, const std::set<coord_t>& vec);
void coutCorners(const int kSquareLen, const std::set<coord_t>& withinBorderCoords, std::set<coord_t>& cornersAdded);
void coutSetAsGrid(const std::set<coord_t>& set, const int kSquareLen);
Direction getOppositeDirection(Direction direction);
void handleFile(std::ifstream& inputFile, square_vect_t& charSquare, const int kSquareLen);
const bool toLog = true; // true false
const int kTestFileLineLen = 5;
const int kFullFileLineLen = 141; // files validated and pre checked for length and valid chars only.
const std::vector<coord_t> kMovements = { {-1, 0}, {0, 1}, {1, 0}, {0, -1} };
const size_t kMovementsSize = kMovements.size();
std::set<coord_t> checkedCoords;
std::map<char, std::set<coord_t>> charCoords;
bool hasDiagonalInSet(const coord_t& nextCoord, const int kSquareLen, const std::set<coord_t>& withinBorderCoords) {
for (int dx = -1; dx <= 1; ++dx) {
for (int dy = -1; dy <= 1; ++dy) {
if (std::abs(dx) != 1 || std::abs(dy) != 1) continue;
coord_t diag = { nextCoord.first + dx, nextCoord.second + dy };
if (isCoordWithinBounds(diag, kSquareLen) && withinBorderCoords.find(diag) != withinBorderCoords.end()) {
return true;
}
}
}
return false;
}
bool isNinetyDegreeOuterBorder(const coord_t& borderCoord, const std::set<coord_t>& registeredBorders) {
coord_t right = { borderCoord.first, borderCoord.second + 1 };
coord_t down = { borderCoord.first + 1, borderCoord.second };
coord_t left = { borderCoord.first, borderCoord.second - 1 };
coord_t up = { borderCoord.first - 1, borderCoord.second };
if (registeredBorders.find(right) != registeredBorders.end() && registeredBorders.find(down) != registeredBorders.end()) return true;
if (registeredBorders.find(right) != registeredBorders.end() && registeredBorders.find(up) != registeredBorders.end()) return true;
if (registeredBorders.find(left) != registeredBorders.end() && registeredBorders.find(down) != registeredBorders.end()) return true;
if (registeredBorders.find(left) != registeredBorders.end() && registeredBorders.find(up) != registeredBorders.end()) return true;
return false;
}
bool isUTurn(const std::set<coord_t>& withinBorderCoords, const coord_t& borderCoord) {
int count = 0;
for (const auto& move : kMovements) {
coord_t ortho = { borderCoord.first + move.first, borderCoord.second + move.second };
if (withinBorderCoords.find(ortho) != withinBorderCoords.end()) ++count;
}
return count >= 3;
}
bool isNinetyDegreeInnerBorder(const std::set<coord_t>& withinBorderCoords, const coord_t& borderCoord) {
int count = 0;
for (const auto& move : kMovements) {
coord_t ortho = { borderCoord.first + move.first, borderCoord.second + move.second };
if (withinBorderCoords.find(ortho) != withinBorderCoords.end()) ++count;
}
return count >= 2;
}
size_t getDiagResultUTurn(const coord_t& border, const std::set<coord_t>& withinBorderCoords)
{
int diagCount = 0;
for (int dx = -1; dx <= 1; ++dx) {
for (int dy = -1; dy <= 1; ++dy) {
if (std::abs(dx) != 1 || std::abs(dy) != 1) continue;
coord_t diag = { border.first + dx, border.second + dy };
if (withinBorderCoords.find(diag) != withinBorderCoords.end()) ++diagCount;
}
}
if (diagCount == 1) return 3;
else if (diagCount == 2) return 3;
else if (diagCount == 3) return 2;
else if (diagCount == 4) return 1;
else {
return 4; // locked in
}
}
size_t getTotalCornersFromFullSet(const int kSquareLen, const std::set<coord_t> withinBorderCoords, std::set<coord_t> allBorders) {
size_t totalCorners = 0;
std::set<coord_t> cornersAdded;
if (withinBorderCoords.size() == 1) {
std::cout << "Single coord (" << (*withinBorderCoords.begin()).first << "," << (*withinBorderCoords.begin()).second << ") adds 4 corners\n";
return 4;
}
for (const auto& border : allBorders) {
size_t cornerCount = 0;
if (isUTurn(withinBorderCoords, border)) {
cornerCount = getDiagResultUTurn(border, withinBorderCoords);
totalCorners += cornerCount;
cornersAdded.insert(border);
std::cout << "UTurn at (" << border.first << "," << border.second << ") adds " << cornerCount << " corners\n";
}
else if (isNinetyDegreeInnerBorder(withinBorderCoords, border)) {
cornerCount = 1;
totalCorners += cornerCount;
cornersAdded.insert(border);
std::cout << "Inner90 at (" << border.first << "," << border.second << ") adds " << cornerCount << " corner\n";
}
else if (isNinetyDegreeOuterBorder(border, allBorders)) {
cornerCount = 1;
totalCorners += cornerCount;
cornersAdded.insert(border);
std::cout << "Outer90 at (" << border.first << "," << border.second << ") adds " << cornerCount << " corner\n";
}
}
if (toLog) coutCorners(kSquareLen, withinBorderCoords, cornersAdded);
return totalCorners;
}
size_t getTotalBorders(const square_vect_t& charSquare, const int kSquareLen, const char charInBorder, const std::set<coord_t>& registeredBorders, std::set<coord_t> withinBorderCoords) {
std::set<coord_t> checkedCorners = registeredBorders;
std::set<coord_t> checkedCornersWithImpact;
for (const auto& border : registeredBorders) { // adds corners not orthogonally connected to an inside char
for (size_t i = 0; i < kMovementsSize; ++i) {
coord_t nextCoord = { border.first + kMovements[i].first, border.second + kMovements[i].second };
if (checkedCorners.find(nextCoord) != checkedCorners.end()) continue;
if (hasCharAtCoord(charSquare, charInBorder, nextCoord, kSquareLen)) {
// if (toLog) std::cout << "nextCoord: (x:" << nextCoord.second << ",y:" << nextCoord.first << ") for " << charInBorder << "\n";
if (withinBorderCoords.find(nextCoord) == withinBorderCoords.end()) {
checkedCorners.insert(nextCoord);
checkedCornersWithImpact.insert(nextCoord);
}
continue;
}
int orthogonalCount = 0;
for (size_t j = 0; j < kMovementsSize; ++j) {
if (!hasDiagonalInSet(nextCoord, kSquareLen, withinBorderCoords)) continue;
coord_t orthoCoord = { nextCoord.first + kMovements[j].first, nextCoord.second + kMovements[j].second };
if (registeredBorders.find(orthoCoord) != registeredBorders.end()) {
++orthogonalCount;
}
}
if (orthogonalCount >= 2) {
checkedCornersWithImpact.insert(nextCoord);
checkedCorners.insert(nextCoord);
}
}
}
if (toLog) coutRegisteredBordersOverlay(registeredBorders, kSquareLen, checkedCornersWithImpact, withinBorderCoords);
std::set<coord_t> allBorders;
allBorders.insert(registeredBorders.begin(), registeredBorders.end());
allBorders.insert(checkedCorners.begin(), checkedCorners.end());
size_t totalCorners = getTotalCornersFromFullSet(kSquareLen, withinBorderCoords, allBorders);
return totalCorners;
}
size_t getAreaPrice(const square_vect_t& charSquare, const int kSquareLen, const coord_t& startCoord) {
size_t entriesChecked = 0;
std::set<coord_t> registeredBorders;
std::vector<std::pair<coord_t, Direction>> coordsWithOrigin;
std::set<coord_t> allCoordsAdded;
coordsWithOrigin.push_back(std::make_pair(startCoord, Direction::None));
allCoordsAdded.insert(startCoord);
while (!coordsWithOrigin.empty()) {
std::pair<coord_t, Direction> top = coordsWithOrigin.back();
coord_t currentCoord = top.first;
Direction prevDirection = top.second;
coordsWithOrigin.pop_back();
if (checkedCoords.find(currentCoord) != checkedCoords.end()) continue;
checkedCoords.insert(currentCoord);
++entriesChecked;
for (int i = 0; i < kMovementsSize; ++i) {
Direction currentDirection = static_cast<Direction>(i);
if (prevDirection == getOppositeDirection(prevDirection)) continue;
coord_t nextCoord = { currentCoord.first + kMovements[i].first, currentCoord.second + kMovements[i].second };
if (hasCharAtCoord(charSquare, charSquare[currentCoord.first][currentCoord.second], nextCoord, kSquareLen)) {
coordsWithOrigin.push_back(std::make_pair(nextCoord, currentDirection));
allCoordsAdded.insert(nextCoord);
}
else {
registeredBorders.insert(nextCoord);
}
}
}
size_t totalBorders = getTotalBorders(charSquare, kSquareLen, charSquare[startCoord.first][startCoord.second], registeredBorders, allCoordsAdded);
//if (toLog)
std::cout << "Total for: " << charSquare[startCoord.first][startCoord.second] << " is " << entriesChecked << " * " << totalBorders << '\n';
return totalBorders * entriesChecked;
}
size_t calculateTotalPrice(const square_vect_t& charSquare, const int kSquareLen) {
size_t total = 0;
for (int row = 0; row < kSquareLen; ++row) {
for (int col = 0; col < kSquareLen; ++col) {
coord_t currentCoord = { row, col };
if (checkedCoords.find(currentCoord) == checkedCoords.end()) {
total += getAreaPrice(charSquare, kSquareLen, currentCoord);
}
}
}
return total;
}
void handleFile(std::ifstream& inputFile, square_vect_t& charSquare, const int kSquareLen)
{
std::pair<int, int> startingGuardCoord;
if (inputFile.is_open()) {
std::string line;
int currentRow = 0;
while (getline(inputFile, line)) {
if (currentRow >= kSquareLen) throw std::runtime_error("Line read out of bounds");
fillLineInVector(line, charSquare, kSquareLen, currentRow);
currentRow++;
}
std::cout << "Finished reading file\n";
inputFile.close(); // automatically happens when going out of scope but no longer needed. More about explicitness.
size_t totalPrice = calculateTotalPrice(charSquare, kSquareLen);
std::cout << "Total price: " << totalPrice;
std::cout << "\nFinished running program";
}
else {
std::cout << "Unable to open file\n";
}
}
bool hasCharAtCoord(const square_vect_t& charSquare, const char prevChar, const coord_t& coordToCheck, const int kSquareLen) {
if (!isCoordWithinBounds(coordToCheck, kSquareLen)) return false;
return (charSquare[coordToCheck.first][coordToCheck.second] == prevChar);
}
bool isCoordWithinBounds(const coord_t& nextCoord, const int kSquareLen)
{
return nextCoord.first >= 0 && nextCoord.first < kSquareLen && nextCoord.second >= 0 && nextCoord.second < kSquareLen;
}
void fillLineInVector(const std::string line, square_vect_t& charSquare, const int kSquareLen, const int currentRow) {
for (int col = 0; col < kSquareLen; ++col) {
charSquare[currentRow][col] = line[col];
}
}
Direction getOppositeDirection(Direction direction) {
switch (direction) {
case Direction::Up: return Direction::Down;
case Direction::Right: return Direction::Left;
case Direction::Down: return Direction::Up;
case Direction::Left: return Direction::Right;
default: return Direction::Invalid;
}
}
//-----------------LOGGING---------------------------------------------------------------
void coutSetContents(const std::string prefix, const std::set<coord_t>& vec) {
std::cout << prefix;
for (const auto& c : vec) {
std::cout << "(" << c.first << "," << c.second << ") ";
}
std::cout << '\n';
}
void coutCorners(const int kSquareLen, const std::set<coord_t>& withinBorderCoords, std::set<coord_t>& cornersAdded) {
int minX = 0, minY = 0, maxX = kSquareLen - 1, maxY = kSquareLen - 1;
for (const auto& c : withinBorderCoords) {
if (c.first < minX) minX = c.first;
if (c.second < minY) minY = c.second;
if (c.first > maxX) maxX = c.first;
if (c.second > maxY) maxY = c.second;
}
for (const auto& c : cornersAdded) {
if (c.first < minX) minX = c.first;
if (c.second < minY) minY = c.second;
if (c.first > maxX) maxX = c.first;
if (c.second > maxY) maxY = c.second;
}
int gridRows = maxX - minX + 3;
int gridCols = maxY - minY + 3;
std::vector<std::vector<char>> grid(gridRows, std::vector<char>(gridCols, '.'));
for (const auto& c : withinBorderCoords) {
int x = c.first - minX + 1, y = c.second - minY + 1;
if (x >= 0 && x < gridRows && y >= 0 && y < gridCols) grid[x][y] = 'c';
}
for (const auto& c : cornersAdded) {
int x = c.first - minX + 1, y = c.second - minY + 1;
if (x >= 0 && x < gridRows && y >= 0 && y < gridCols) grid[x][y] = 'X';
}
std::cout << "Corners:\n";
for (const auto& row : grid) {
for (const auto& cell : row) std::cout << cell;
std::cout << '\n';
}
}
void coutSetAsGrid(const std::set<coord_t>& set, const int kSquareLen) {
int gridLen = kSquareLen + 2;
std::vector<std::vector<char>> grid(gridLen, std::vector<char>(gridLen, '.'));
for (const auto& coord : set) {
int x = coord.first + 1, y = coord.second + 1;
if (x >= 0 && x < gridLen && y >= 0 && y < gridLen) grid[x][y] = '#';
}
for (const auto& row : grid) {
for (const auto& cell : row) std::cout << cell;
std::cout << '\n';
}
}
void coutRegisteredBordersOverlay(const std::set<coord_t>& registeredBorders, const int kSquareLen, const std::set<coord_t>& priorityCoords, const std::set<coord_t>& otherCoords) {
int gridLen = kSquareLen + 2;
std::vector<std::vector<char>> grid(gridLen, std::vector<char>(gridLen, '.'));
for (const auto& coord : otherCoords) {
int x = coord.first + 1, y = coord.second + 1;
if (x >= 0 && x < gridLen && y >= 0 && y < gridLen) grid[x][y] = 'c';
}
for (const auto& coord : registeredBorders) {
int x = coord.first + 1, y = coord.second + 1;
if (x >= 0 && x < gridLen && y >= 0 && y < gridLen) grid[x][y] = '#';
}
for (const auto& coord : priorityCoords) {
int x = coord.first + 1, y = coord.second + 1;
if (x >= 0 && x < gridLen && y >= 0 && y < gridLen) {
if (grid[x][y] == '#') grid[x][y] = '&';
else grid[x][y] = '+';
}
}
for (const auto& row : grid) {
for (const auto& cell : row) std::cout << cell;
std::cout << '\n';
}
}
void coutCharSquareWithChecked(const square_vect_t& charSquare, const int kSquareLen) {
for (int row = 0; row < kSquareLen; ++row) {
for (int col = 0; col < kSquareLen; ++col) {
coord_t currentCoord = { row, col };
if (checkedCoords.find(currentCoord) != checkedCoords.end()) {
std::cout << '.';
}
else {
std::cout << charSquare[row][col];
}
}
std::cout << '\n';
}
std::cout << '\n';
}
//-----------------LOGGING---------------------------------------------------------------
}
void dayTwelvePartTwo() {
std::system("cls"); // clear terminal pre-boot
std::cout << "Running program DayTwelvePartTwo" << '\n';
const bool isFullFile = false; // true false
const int kSquareLen = isFullFile ? kFullFileLineLen : kTestFileLineLen;
square_vect_t charSquare(kSquareLen, std::vector<char>(kSquareLen));
std::string line;
std::ifstream inputFile;
if (isFullFile) {
std::cout << "Using full file\n\n";
}
else {
std::cout << "Using test file\n\n";
}
(isFullFile) ? inputFile.open("dayTwelveFull.txt") : inputFile.open("dayTwelveTest.txt");
handleFile(inputFile, charSquare, kSquareLen);
}
}
// 928307 too low
// 930118 too high
// 930107 not right
// 931793 not right
// 937032 from test, unsubmitted