-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirections-reduction.cpp
More file actions
32 lines (26 loc) · 1.03 KB
/
Copy pathdirections-reduction.cpp
File metadata and controls
32 lines (26 loc) · 1.03 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
#include <vector>
#include <string>
#include <unordered_map>
class DirReduction
{
public:
static auto dirReduc(std::vector<std::string> &arr) -> std::vector<std::string>;
};
[[nodiscard]] auto DirReduction::dirReduc(std::vector<std::string> &arr) -> std::vector<std::string> {
if (arr.size() < 2) {return arr;}
auto oposite_to = std::unordered_map<std::string, std::string>{{"NORTH", "SOUTH"},
{"SOUTH", "NORTH"},
{"EAST", "WEST"},
{"WEST", "EAST"},};
auto after_reduction = std::vector<std::string>{};
after_reduction.reserve(arr.size());
for (auto const& elem : arr) {
if (after_reduction.empty() || (after_reduction.back() != oposite_to.at(elem))) {
after_reduction.push_back(elem);
}
else {
after_reduction.pop_back();
}
}
return after_reduction;
}