-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.cpp
More file actions
197 lines (169 loc) · 4.86 KB
/
input.cpp
File metadata and controls
197 lines (169 loc) · 4.86 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
#include <cassert>
#include <deque>
#include <filesystem>
#include <list>
#include <stdexcept>
#include <vector>
#include "input.hpp"
namespace utils {
using std::size_t;
Reader::Reader(std::filesystem::path input) : input(input) {}
std::vector<std::string> Reader::get_lines() {
std::string line;
std::vector<std::string> result;
std::ifstream input_file(input);
if (std::filesystem::exists(input)) {
while (std::getline(input_file, line)) {
result.push_back(line);
}
input_file.close();
} else {
std::stringstream ss;
ss << "Input file " << input << " doesn't exist.";
throw std::runtime_error(std::string(ss.str()));
}
return result;
}
std::vector<std::string> split_string(const std::string &input,
const char &delimiter) {
std::vector<std::string> splits;
std::string buffer;
for (auto character : input) {
if (character == delimiter) {
if (buffer.size() > 0) {
splits.push_back(buffer);
}
buffer = "";
} else {
buffer += character;
}
}
if (buffer.size() > 0) {
splits.push_back(buffer);
}
return splits;
}
std::vector<std::string> split_string(const std::string &input,
const std::string &delimiter) {
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
std::string token;
std::vector<std::string> res;
while ((pos_end = input.find(delimiter, pos_start)) != std::string::npos) {
token = input.substr(pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back(token);
}
res.push_back(input.substr(pos_start));
return res;
}
void replace_all_substrings(std::string *input, const std::string &search,
const std::string &replacement) {
while (input->find(search) != std::string::npos) {
input->replace(input->find(search), search.size(), replacement);
}
}
template<> std::vector<int> extract_numbers<int>(const std::string& input) {
std::vector<int> result;
std::string buffer;
for (auto c : input) {
if (buffer.size() == 0 && c == '-') {
buffer += c;
} else if (buffer.size() == 0 && c == '+') {
continue;
} else if (std::isdigit(c)) {
buffer += c;
} else {
if (buffer.size() > 0) {
result.push_back(std::stoi(buffer));
buffer = "";
}
}
}
if (buffer.size() > 0) {
result.push_back(std::stoi(buffer));
}
return result;
}
template<> std::vector<int64_t> extract_numbers<int64_t>(const std::string& input) {
std::vector<int64_t> result;
std::string buffer;
for (auto c : input) {
if (buffer.size() == 0 && c == '-') {
buffer += c;
} else if (buffer.size() == 0 && c == '+') {
continue;
} else if (std::isdigit(c)) {
buffer += c;
} else {
if (buffer.size() > 0) {
result.push_back(std::stoll(buffer));
buffer = "";
}
}
}
if (buffer.size() > 0) {
result.push_back(std::stoll(buffer));
}
return result;
}
template <typename T>
std::vector<T> rotate_vector(const std::vector<T> &input, const int &rotation) {
std::vector<T> result;
size_t size = input.size();
result.resize(size);
int steps;
if (rotation >= 0) {
steps = rotation % size;
for (size_t index{0}; index < size; index++) {
result[(index + steps) % size] = input[index];
}
} else {
steps = -rotation % size;
for (size_t index{0}; index < size; index++) {
result[index] = input[(index + steps) % size];
}
}
return result;
}
template std::vector<bool> rotate_vector<bool>(const std::vector<bool> &,
const int &);
template std::vector<char> rotate_vector<char>(const std::vector<char> &,
const int &);
template std::vector<int> rotate_vector<int>(const std::vector<int> &,
const int &);
template <typename T> T pow(T base, T exponent) {
assert(exponent >= 0);
if (exponent == 0) {
return 1;
} else {
return base * pow(base, exponent - 1);
}
}
template int pow<int>(int, int);
template size_t pow<size_t>(size_t, size_t);
template int64_t pow<int64_t>(int64_t, int64_t);
template <typename T> T factorial(T n) {
if (n <= 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
template int factorial<int>(int);
template int64_t factorial<int64_t>(int64_t);
template size_t factorial<size_t>(size_t);
template <typename T> std::string stringify(T input) {
std::string result;
for (auto c : input) {
result += c;
}
return result;
}
template std::string stringify(std::vector<char>);
template std::string stringify(std::deque<char>);
template <class T> inline int sign(const T &value) {
return (T(0) < value) - (value < T(0));
}
template int sign(const int &);
template int sign(const int64_t &);
} // namespace utils