-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaySix.cpp
More file actions
159 lines (130 loc) · 5.07 KB
/
daySix.cpp
File metadata and controls
159 lines (130 loc) · 5.07 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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
namespace DaySix
{
namespace {
using square_vect_t = std::vector<std::vector<char>>;
void coutSquareVector(std::string prefix, const square_vect_t& charSquare); // function declaration on top of the file for the less important functions
int calculateTotal(square_vect_t& charSquare);
enum class Direction { Up, Right, Down, Left };
std::pair<int, int> getNextCoord(std::pair<int, int> coordCopy, const Direction direction);
bool isCoordWithinBounds(std::pair<int, int>& nextCoord, const int kSquareLen);
const bool toLog = false; // true false
const int kTestFileLineLen = 10;
const int kFullFileLineLen = 130; // files validated and pre checked for length and valid chars only.
const char guardChar = '^';
const char obstacleChar = '#';
const char emptySpaceChar = '.';
const char crossedSpaceChar = 'X';
void runGuardRoute(square_vect_t& charSquare, std::pair<int, int>& guardCoord, const int kSquareLen)
{
Direction currentDirection = Direction::Up;
int kMaxIterations = 1'000'000;
int iterations = 0;
while (true) {
if (++iterations > kMaxIterations) {
throw std::runtime_error("Past max_iter");
}
// if (toLog) coutSquareVector("new state: ", charSquare);
charSquare[guardCoord.first][guardCoord.second] = crossedSpaceChar;
std::pair<int, int> nextCoord = getNextCoord(guardCoord, currentDirection);
if (!isCoordWithinBounds(nextCoord, kSquareLen)) break;
char nextCoordChar = charSquare[nextCoord.first][nextCoord.second];
switch (nextCoordChar) {
case obstacleChar:
currentDirection = static_cast<Direction>((static_cast<int>(currentDirection) + 1) % 4);
break;
case emptySpaceChar:
case crossedSpaceChar:
guardCoord = nextCoord;
break;
default:
if (toLog) std::cout << "Invalid char at " << nextCoord.first << '-' << nextCoord.second << ": " << nextCoordChar;
throw std::runtime_error("Invalid char found");
}
}
}
void fillLineInVector(const std::string line, const int kSquareLen, square_vect_t& charSquare, std::pair<int, int>& guardCoord, const int currentRow) {
for (int col = 0; col < kSquareLen; ++col) {
charSquare[currentRow][col] = line[col];
if (line[col] == guardChar) guardCoord = std::pair<int, int>(currentRow, col);
}
}
void handleFile(std::ifstream& inputFile, const int kSquareLen, square_vect_t& charSquare, std::pair<int, int>& guardCoord)
{
if (inputFile.is_open()) {
std::string line;
int currentRow = 0;
while (getline(inputFile, line)) {
if (currentRow >= kSquareLen) throw std::runtime_error::runtime_error("line read out of bounds");
fillLineInVector(line, kSquareLen, charSquare, guardCoord, currentRow);
++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) coutSquareVector("Starting state: ", charSquare);
runGuardRoute(charSquare, guardCoord, kSquareLen);
if (toLog) coutSquareVector("End state: ", charSquare);
int total = calculateTotal(charSquare);
std::cout << "Total: " << total << '\n';
std::cout << "Finished running program\n";
}
else {
std::cout << "Unable to open file\n";
}
}
bool isCoordWithinBounds(std::pair<int, int>& nextCoord, const int kSquareLen)
{
return nextCoord.first >= 0 && nextCoord.first < kSquareLen && nextCoord.second >= 0 && nextCoord.second < kSquareLen;
}
std::pair<int, int> getNextCoord(std::pair<int, int> coordCopy, const Direction direction)
{
switch (direction)
{
case (Direction::Up):
return std::pair<int, int>(coordCopy.first - 1, coordCopy.second);
case (Direction::Right):
return std::pair<int, int>(coordCopy.first, coordCopy.second + 1);
case (Direction::Down):
return std::pair<int, int>(coordCopy.first + 1, coordCopy.second);
case (Direction::Left):
return std::pair<int, int>(coordCopy.first, coordCopy.second - 1);
default:
throw std::runtime_error::runtime_error("Invalid enum value");
}
}
int calculateTotal(square_vect_t& charSquare)
{
int total = 0;
for (const auto& row : charSquare) {
for (const auto& c : row) {
if (c == crossedSpaceChar) ++total;
}
}
return total;
}
void coutSquareVector(std::string prefix, const square_vect_t& charSquare) {
std::cout << prefix << '\n';
for (const auto& row : charSquare) {
for (const auto& ch : row) {
std::cout << ch;
}
std::cout << '\n';
}
std::cout << '\n';
}
}
void daySix() {
std::cout << "Running program Day Six" << '\n';
const bool isFullFile = true; // true false
const int kSquareLen = isFullFile ? kFullFileLineLen : kTestFileLineLen;
square_vect_t charSquare(kSquareLen, std::vector<char>(kSquareLen));
std::pair<int, int> guardCoord;
std::string line;
std::ifstream inputFile;
(isFullFile) ? inputFile.open("daySixFull.txt") : inputFile.open("daySixTest.txt");
handleFile(inputFile, kSquareLen, charSquare, guardCoord);
}
}