-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
95 lines (85 loc) · 2.13 KB
/
utils.cpp
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
#include <fstream>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <tuple>
#include <vector>
#include <cmath>
using namespace std;
template<typename T>
T id(T v) {
return v;
}
template <typename K, typename V>
ostream &operator<<(ostream &os, const unordered_map<K, V> &m)
{
for (const pair<K, V> &p : m)
{
os << "{" << p.first << ": " << p.second << "}\n";
}
return os;
}
template <typename K, typename V, typename H>
ostream &operator<<(ostream &os, const unordered_map<K, V, H> &m)
{
for (const pair<K, V> &p : m)
{
os << "{" << p.first << ": " << p.second << "}\n";
}
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v)
{
os << "{\n";
for (const auto &e : v)
{
os << " " << e << ",\n";
}
os << "}\n";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &v)
{
os << "{\n";
for (const auto &e : v)
{
os << " " << e << ",\n";
}
os << "}\n";
return os;
}
string trim(const string &s)
{
auto wsfront = find_if_not(s.begin(), s.end(), [](int c) { return isspace(c); });
return string(wsfront, find_if_not(s.rbegin(), string::const_reverse_iterator(wsfront), [](int c) { return isspace(c); }).base());
}
tuple<string, string> cut(const string &s, const string delimiter) {
const auto delimiterIndex = s.find(delimiter);
const string firstPart = s.substr(0, delimiterIndex);
const string secondPart = trim(s.substr(delimiterIndex + delimiter.size()));
return make_tuple(firstPart, secondPart);
}
vector<string> split(const string &s, const string delimiter) {
size_t delimiterIndex = 0;
string str = s;
vector<string> tokens;
while ((delimiterIndex = str.find(delimiter)) != string::npos) {
const string token = str.substr(0, delimiterIndex);
tokens.push_back(token);
str = str.substr(delimiterIndex + delimiter.length());
}
tokens.push_back(str);
return tokens;
}
vector<string> getPuzzleInput(const string &path)
{
ifstream inputFile(path);
vector<string> circuit;
for (string line; getline(inputFile, line);)
{
circuit.push_back(line);
}
return circuit;
}