-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdayFifteen.cpp
More file actions
262 lines (230 loc) · 7.38 KB
/
Copy pathdayFifteen.cpp
File metadata and controls
262 lines (230 loc) · 7.38 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
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
namespace DayFifteen
{
// [0,0]
// [xLen-1,yLen-1]
namespace {
#pragma region charDef
namespace GridChar {
constexpr char Wall = '#';
constexpr char Empty = '.';
constexpr char Box = 'O';
constexpr char Bot = '@';
constexpr char Up = '^';
constexpr char Right = '>';
constexpr char Down = 'v';
constexpr char Left = '<';
}
#pragma endregion
using coord_t = std::pair<long, long>;
using char_square_t = std::vector<std::vector<char>>; // heap, too large for the stack
enum Direction { Up, Right, Down, Left };
void initializeGrid(const long kLen);
void handleLine(std::string line);
void coutCharSquareAsGrid(const std::string& prefix);
void parseStartingStateLine(const std::string& line, const long kLen, long& currentRow);
coord_t getNextCoord(const char gridChar, const coord_t& pos);
bool isValidCoord(const long kLen, const coord_t& coord);
Direction getDirectionFromAToB(const coord_t& a, const coord_t& b);
void updateBotPos(const coord_t& newPos);
char directionToChar(Direction dir);
long calcTotal(const long kLen);
const bool toLog = false; // true false
const int kMaxIter = 100'000;
const long kLenTest = 10;
const long kLenFull = 50;
char_square_t grid;
coord_t botPos{ -1, -1 };
void moveBoxes(const Direction boxDir, const std::pair<coord_t,coord_t> startAndEndBox)
{
grid[startAndEndBox.first.first][startAndEndBox.first.second] = GridChar::Empty;
grid[startAndEndBox.second.first][startAndEndBox.second.second] = GridChar::Box;
grid[botPos.first][botPos.second] = GridChar::Empty;
botPos = startAndEndBox.first;
grid[botPos.first][botPos.second] = GridChar::Bot;
if (toLog) coutCharSquareAsGrid("state: \n");
}
void handleBoxMovement(const coord_t firstBoxPos, const Direction boxDir, const long kLen)
{
std::pair<coord_t, coord_t> startAndEndBox;
startAndEndBox.first = firstBoxPos;
coord_t currentPos = firstBoxPos;
char dirChar = directionToChar(boxDir);
bool endCharIsEmpty = false;
int iter = 0;
while (isValidCoord(kLen, currentPos) || iter > kMaxIter) {
++iter;
char currentChar = grid[currentPos.first][currentPos.second];
if (currentChar == GridChar::Wall || currentChar == GridChar::Empty) {
endCharIsEmpty = (currentChar == GridChar::Empty);
startAndEndBox.second = currentPos;
break;
}
currentPos = getNextCoord(dirChar, currentPos);
}
if (endCharIsEmpty) {
moveBoxes(boxDir, startAndEndBox);
}
else {
if (toLog) std::cout << "Wall end found at [" << currentPos.first << "," << currentPos.second << "]\n";
}
}
void handleDirections(const std::string& directions, const long kLen)
{
if (toLog) std::cout << directions << '\n';
for (const char dir : directions)
{
if (toLog) std::cout << dir << '\n';
coord_t nextBotPos = getNextCoord(dir, botPos);
if (!isValidCoord(kLen, nextBotPos)) throw std::runtime_error("Bot wants to escape wall containment");
char nextPosChar = grid[nextBotPos.first][nextBotPos.second];
switch (nextPosChar) {
case GridChar::Wall:
if (toLog) std::cout << "Wall at [" << nextBotPos.first << "," << nextBotPos.second << "]\n";
continue;
case GridChar::Box:
handleBoxMovement(nextBotPos, getDirectionFromAToB(botPos, nextBotPos), kLen);
continue;
case GridChar::Empty:
updateBotPos(nextBotPos);
continue;
}
}
}
void handleFile(std::ifstream& inputFile, const long kLen)
{
if (inputFile.is_open()) {
initializeGrid(kLen); // & botpos init too
std::string line;
bool afterBlankLine = false;
long currentRow = 0;
while (getline(inputFile, line)) {
if (line.empty()) {
afterBlankLine = true;
continue;
}
if (!afterBlankLine) {
parseStartingStateLine(line, kLen, currentRow);
}
else {
if (toLog) coutCharSquareAsGrid("Start: \n");
handleDirections(line, kLen); // 20 000 line length full
if (toLog) coutCharSquareAsGrid("End: \n");
}
}
std::cout << "Finished reading file\n";
inputFile.close(); // automatically happens when going out of scope but no longer needed. More about explicitness.
std::cout << "Total: " << calcTotal(kLen) << '\n';
std::cout << "\nFinished running program\n";
}
else {
std::cout << "Unable to open file\n";
}
}
long calcTotal(const long kLen)
{
const long gpsMult = 100;
long total = 0;
for (long y = 0; y < kLen; ++y) {
for (long x = 0; x < kLen; ++x) {
if (grid[y][x] == GridChar::Box) {
if (toLog) std::cout << "Box at [" << y << "," << x << "] adds " << (y * gpsMult) + x << '\n';
total += (y * gpsMult) + x;
}
}
}
return total;
}
#pragma region helper
void updateBotPos(const coord_t& newPos) {
grid[botPos.first][botPos.second] = GridChar::Empty;
grid[newPos.first][newPos.second] = GridChar::Bot;
botPos = newPos;
if (toLog) coutCharSquareAsGrid("++E: State: \n");
}
char directionToChar(Direction dir) {
switch (dir) {
case Direction::Up: return GridChar::Up;
case Direction::Right: return GridChar::Right;
case Direction::Down: return GridChar::Down;
case Direction::Left: return GridChar::Left;
}
std::cout << dir;
throw std::runtime_error("Invalid dir passed");
}
Direction getDirectionFromAToB(const coord_t& a, const coord_t& b) {
long dx = b.second - a.second;
long dy = b.first- a.first;
if (abs(dx) > abs(dy)) {
return dx > 0 ? Direction::Right : Direction::Left;
}
else {
return dy > 0 ? Direction::Down : Direction::Up;
}
}
bool isValidCoord(const long kLen, const coord_t& coord) {
return coord.first >= 0 && coord.first < kLen && coord.second >= 0 && coord.second < kLen;
}
coord_t getNextCoord(const char gridChar, const coord_t& pos) {
coord_t newPos = pos;
switch (gridChar) {
case GridChar::Up: newPos.first--; break;
case GridChar::Right: newPos.second++; break;
case GridChar::Down: newPos.first++; break;
case GridChar::Left: newPos.second--; break;
}
return newPos;
}
#pragma endregion
#pragma region cout
void coutCharSquareAsGrid(const std::string& prefix) {
std::cout << prefix;
for (const auto& row : grid) {
for (const auto& c : row) {
std::cout << c;
}
std::cout << '\n';
}
std::cout << '\n';
}
#pragma endregion
#pragma region startState
void parseStartingStateLine(const std::string& line, const long kLen, long& currentRow)
{
if (grid.empty() || currentRow >= grid.size()) {
return;
}
for (long x = 0; x < kLen; ++x) {
grid[currentRow][x] = line[x];
if (line[x] == GridChar::Bot) {
botPos = { x, currentRow };
}
}
++currentRow;
}
void initializeGrid(const long kLen) {
grid.resize(kLen, std::vector<char>(kLen, GridChar::Empty));
}
#pragma endregion
}
void dayFifteen() {
std::system("cls"); // clear terminal pre-boot
std::cout << "Running program DayFifteen" << '\n';
const bool isFullFile = true; // true false
const long kLen = isFullFile ? kLenFull : kLenTest;
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("dayFifteenFull.txt") : inputFile.open("dayFifteenTest.txt");
handleFile(inputFile, kLen);
}
}