-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdayFourPartTwo.cpp
More file actions
192 lines (163 loc) · 6.13 KB
/
dayFourPartTwo.cpp
File metadata and controls
192 lines (163 loc) · 6.13 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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip> // just for cleaner logging
#include <utility>
namespace DayFourPartTwo
{
namespace { // approach: RAM and simplicity over minimal RAM solution without large arrays
using square_arr_t = std::vector<std::vector<char>>;
using coordinates_t = std::vector<std::pair<int, int>>;
const bool toLog = false; // true false
const int kTestFileLineLen = 10;
const int kFullFileLineLen = 140; // files validated and pre checked for length and valid chars only.
const char kMiddleChar = 'A';
const char kThirdChar = 'M';
const char kFourthChar = 'S';
bool isValidCoord(std::pair<int, int> coord, const int kArraySize)
{
if (coord.first < 0 || coord.first >= kArraySize || coord.second < 0 || coord.second >= kArraySize) {
return false;
}
return true;
}
bool areSurroundingCoordsValid(std::pair<int, int> coordCopy, const int kArraySize)
{
std::pair<int, int> coordToCheck = coordCopy;
coordToCheck.first -= 1;
coordToCheck.second += 1;
if (!isValidCoord(coordToCheck, kArraySize)) return false;
coordToCheck = coordCopy;
coordToCheck.first += 1;
coordToCheck.second += 1;
if (!isValidCoord(coordToCheck, kArraySize)) return false;
coordToCheck = coordCopy;
coordToCheck.first -= 1;
coordToCheck.second -= 1;
if (!isValidCoord(coordToCheck, kArraySize)) return false;
coordToCheck = coordCopy;
coordToCheck.first += 1;
coordToCheck.second -= 1;
if (!isValidCoord(coordToCheck, kArraySize)) return false;
return true;
}
bool isValidOppositeDiagonalChar(char sourceChar, char oppositeToCheck)
{
if (sourceChar == kThirdChar)
{
return oppositeToCheck == kFourthChar;
}
return oppositeToCheck == kThirdChar;
}
bool isValidDiagonalChar(char c)
{
return c == kThirdChar || c == kFourthChar;
}
bool isDiagonalValid(const square_arr_t charSquare, const int kArraySize, const std::pair<int, int> middleCharCoord, const bool startTopLeft)
{
if (!areSurroundingCoordsValid(middleCharCoord, kArraySize)) return false;
if (startTopLeft)
{
std::pair<int, int> coordToCheck = middleCharCoord;
coordToCheck.first -= 1;
coordToCheck.second -= 1;
char topLeftChar = charSquare[coordToCheck.first][coordToCheck.second];
if (!isValidDiagonalChar(topLeftChar)) return false;
coordToCheck = middleCharCoord;
coordToCheck.first += 1;
coordToCheck.second += 1;
if (!isValidOppositeDiagonalChar(topLeftChar, charSquare[coordToCheck.first][coordToCheck.second])) return false;
}
else
{
std::pair<int, int> coordToCheck = middleCharCoord;
coordToCheck.first += 1;
coordToCheck.second -= 1;
char topRightChar = charSquare[coordToCheck.first][coordToCheck.second];
if (!isValidDiagonalChar(charSquare[coordToCheck.first][coordToCheck.second])) return false;
coordToCheck = middleCharCoord;
coordToCheck.first -= 1;
coordToCheck.second += 1;
if (!isValidOppositeDiagonalChar(topRightChar, charSquare[coordToCheck.first][coordToCheck.second])) return false;
}
return true;
}
bool isMiddleCharInValidX(const square_arr_t charSquare, const int kArraySize, const std::pair<int, int> middleCharCoord)
{
if (!isDiagonalValid(charSquare, kArraySize, middleCharCoord, true)) return false;
if (!isDiagonalValid(charSquare, kArraySize, middleCharCoord, false)) return false;
return true;
}
int calculateTotal(const square_arr_t charSquare, const int kArraySize, const coordinates_t middleCharCoords)
{
int totalFinds = 0;
for (const auto& coord : middleCharCoords)
{
if (isMiddleCharInValidX(charSquare, kArraySize, coord)) totalFinds++;
if (toLog) std::cout << coord.first << '-' << coord.second <<'\n';
}
return totalFinds;
}
void fillLineInVector(const std::string line, square_arr_t& charSquare, const int currentRow, coordinates_t& middleCharCoords, const int kArraySize) {
for (int col = 0; col < kArraySize; ++col) {
charSquare[currentRow][col] = line[col];
if (line[col] == kMiddleChar) middleCharCoords.push_back(std::pair<int, int>(currentRow, col));
}
}
void handleFile(std::ifstream& inputFile, const int kArraySize, square_arr_t& charSquare, coordinates_t& middleCharCoords)
{
if (inputFile.is_open()) {
std::string line;
int currentRow = 0;
while (getline(inputFile, line)) {
if (currentRow >= kArraySize) throw std::runtime_error::runtime_error("line read out of bounds");
fillLineInVector(line, charSquare, currentRow, middleCharCoords, kArraySize);
++currentRow;
}
std::cout << "Finished reading file\n";
inputFile.close(); // automatically happens when going out of scope but no longer needed. More about explicitness.
if (toLog) {
std::cout << "Full Content of 2D Array:\n";
for (const auto& row : charSquare) {
for (const char& ch : row) {
std::cout << ch;
}
std::cout << '\n';
}
}
int total = calculateTotal(charSquare, kArraySize, middleCharCoords);
std::cout << "Total: " << total << '\n';
if (toLog) {
std::cout << "Valid Coordinates:\n";
for (const auto& coord : middleCharCoords) {
if (isMiddleCharInValidX(charSquare, kArraySize, coord)) {
std::cout << "Coordinate: (" << coord.first << ", " << coord.second << ")\n";
int row = coord.first;
std::cout << "Row " << row << ": ";
for (const char& ch : charSquare[row]) {
std::cout << ch;
}
std::cout << '\n';
}
}
}
std::cout << "Finished running program\n";
}
else {
std::cout << "Unable to open file\n";
}
}
}
void dayFourPartTwo() {
std::cout << "Running program dayFour Part Two" << '\n';
const bool isTestFile = false; // true false
const int kArraySize = isTestFile ? kTestFileLineLen : kFullFileLineLen;
square_arr_t charSquare(kArraySize, std::vector<char>(kArraySize));
coordinates_t middleCharCoords;
std::string line;
std::ifstream inputFile;
(isTestFile) ? inputFile.open("dayFourTest.txt") : inputFile.open("dayFourFull.txt");
handleFile(inputFile, kArraySize, charSquare, middleCharCoords);
}
}