-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathannotate.cpp
More file actions
232 lines (217 loc) · 6.04 KB
/
annotate.cpp
File metadata and controls
232 lines (217 loc) · 6.04 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
#include <iostream>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <exception>
#include <memory>
#include <random>
#include <chrono>
#include <numeric>
#include <iterator>
#include <iomanip>
#include "cxxopts.hpp"
class CMDOptions {
private:
bool mHelp = false;
std::string mFilePath;
cxxopts::Options options;
bool parse(int argc, char* argv[]) {
try {
options.parse(argc, argv);
return true;
} catch(const cxxopts::OptionException& e) {
std::cerr << "error parsing options: " << e.what() << std::endl;
return false;
}
}
public:
CMDOptions() : options("annotate", "Pathfinding annotator for maps") {
options.add_options()
("f,file", "Appends results to file", cxxopts::value<std::string>(mFilePath)
->default_value("ann.txt"))
("h,help", "Prints help", cxxopts::value<bool>(mHelp))
;
}
bool parseCMDLine(int argc, char* argv[]) {
if (!parse(argc, argv)) {
return false;
}
if (mFilePath.length() == 0) {
return false;
}
return true;
}
std::string getFilePath() const {return mFilePath;}
bool isHelp() const {return mHelp;}
std::string helpMessage() const {return options.help({""});}
void print() const {
std::cout << "options = {"
<< "\n file: " << mFilePath
<< ",\n help: " << mHelp
<< "\n}" << std::endl;
}
};
template <typename Sequence>
void print(const Sequence& sequence)
{
std::cout << "[";
bool notFirst = false;
for (const auto& s : sequence) {
if (notFirst) {
std::cout << ", ";
}
std::cout << s;
notFirst = true;
}
std::cout << "]\n";
}
std::vector<int> readInput() {
std::vector<int> v;
std::string line;
std::getline(std::cin, line);
std::istringstream ss{line};
char c;
ss >> c;
if (c != '[') {
throw cxxopts::OptionException("Input error. Should start with '['.");
}
int i;
c = ',';
while (c != ']') {
if (c != ',') {
throw cxxopts::OptionException("Input error. Should separate elements by ','.");
}
ss >> i >> c;
v.push_back(i);
}
return v;
}
std::pair<int, int> prettyPrintMap(const std::vector<int>& map) {
std::vector<char> drawMap;
for (auto it = map.cbegin(); it != map.cend(); ++it) {
if (*it == 0) {
drawMap.push_back('.');
continue;
}
if (*it == 1) {
drawMap.push_back('#');
continue;
}
if (*it == -1) {
drawMap.push_back('T');
}
}
std::cout << "\nCoordinate system: (x, y)\n\n"
<< " (0,-1) (1,-1)\n"
<< " (-1,0) (0,0) (1, 0)\n"
<< " (-1,1) (0,1)\n";
std::cout << "\nAnnotate the best first path from S to T.\n\n";
int mSize = (-3 + std::sqrt(9 - 4 * 3 * (1 - drawMap.size()))) / 6;
int counter = 0;
int Tx = 0;
int Ty = 0;
for (int y = -mSize; y <= 0; ++y) {
for (int x = -mSize; x <= mSize; ++x) {
int z = -x - y;
if (z > mSize || z < -mSize) {
// We are off the grid
std::cout << " ";
continue;
}
if (x == 0 && y == 0) {
std::cout << " S";
++counter;
continue;
}
if (drawMap[counter] == 'T') {
Tx = x;
Ty = y;
}
std::cout << " " << drawMap[counter];
++counter;
}
std::cout << "\n";
}
for (int y = 1; y <= mSize; ++y) {
for (int i = 0; i < y; ++i) {
std::cout << " ";
}
for (int x = -mSize; x <= mSize; ++x) {
int z = -x - y;
if (z > mSize || z < -mSize) {
// We are off the grid
continue;
}
if (drawMap[counter] == 'T') {
Tx = x;
Ty = y;
}
std::cout << " " << drawMap[counter];
++counter;
}
std::cout << "\n";
}
return std::make_pair(Tx, Ty);
}
std::pair<int, int> annotate(std::pair<int, int> target) {
std::cout << "\nT = (" << target.first << ", " << target.second << ")\n";
std::cout << "End coordinates of path, as 'x, y' (default is T): ";
std::string line;
std::getline(std::cin, line);
std::istringstream ss{line};
char c;
if (line.length() == 0) {
return target;
}
ss >> target.first >> c;
if (c != ',') {
throw cxxopts::OptionException("Input error. Should separate elements by ','.");
}
ss >> target.second;
return target;
}
void writeToFile(const std::string& filePath, const std::vector<int>& map, std::pair<int, int> P) {
auto fs = std::ofstream(filePath, std::ios::app|std::ios::out);
bool notFirst = false;
for (auto it = map.cbegin(); it != map.cend(); ++it) {
if (notFirst) {
fs << " ";
}
fs << *it;
notFirst = true;
}
int mSize = (-3 + std::sqrt(9 - 4 * 3 * (1 - map.size()))) / 6;
for (int y = -mSize; y <= mSize; ++y) {
for (int x = -mSize; x <= mSize; ++x) {
int z = -x - y;
if (z > mSize || z < -mSize) {
continue;
}
fs << " ";
if (x == P.first && y == P.second) {
fs << "1";
continue;
}
fs << "0";
}
}
fs << "\n";
}
int main(int argc, char* argv[]) {
CMDOptions opts;
if (!opts.parseCMDLine(argc, argv)) {
return 1;
}
if (opts.isHelp())
{
std::cout << opts.helpMessage() << std::endl;
return 0;
}
// opts.print();
auto map = readInput();
auto T = prettyPrintMap(map);
auto P = annotate(T);
writeToFile(opts.getFilePath(), map, P);
return 0;
}