-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuncompress.cpp
More file actions
205 lines (148 loc) · 4.92 KB
/
Copy pathuncompress.cpp
File metadata and controls
205 lines (148 loc) · 4.92 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
#include <string>
#include <vector>
#include <regex>
std::vector<std::string> split(const std::string &s, const std::string &delimiter);
class Inflatable {
private:
static Inflatable *create_from(const std::string &s);
public:
[[nodiscard]] virtual std::vector<int> inflate() const = 0;
static std::vector<Inflatable *> map_from(const std::vector<std::string> &strings) {
std::vector<Inflatable *> created;
for (const std::string &string: strings) {
created.push_back(Inflatable::create_from(string));
}
return created;
}
};
std::vector<int> inflate_all(std::vector<Inflatable *> &inflatables);
std::vector<int> uncompress(std::string music) {
const std::vector<std::string> &compressed_values = split(music, ",");
std::vector<Inflatable *> inflatables = Inflatable::map_from(compressed_values);
return inflate_all(inflatables);
}
std::vector<int> inflate_all(std::vector<Inflatable *> &inflatables) {
std::vector<int> all_inflated;
for (Inflatable *i: inflatables) {
const std::vector<int> &inflated_items = i->inflate();
for (const int inflated_item: inflated_items) {
all_inflated.push_back(inflated_item);
}
}
return all_inflated;
}
template<typename T>
int signum(T val) {
if (T(0) > val) {
return -1;
}
if (T(0) < val) {
return 1;
}
return 0;
}
class SameInterval : public Inflatable {
private:
static std::regex interval_regex;
const int _start;
const int _end;
const int _step;
SameInterval(int start, int end, int step) : _start(start), _end(end), _step(step) {}
public:
[[nodiscard]] std::vector<int> inflate() const override {
std::vector<int> inflated;
int i = _start;
for (; i != _end; i += _step) {
inflated.push_back(i);
}
inflated.push_back(i);
return inflated;
}
static Inflatable *create_from(const std::string &s) {
std::smatch match;
if (!std::regex_search(s, match, interval_regex)) {
throw std::exception();
}
const int start = std::stoi(match[1]);
const int end = std::stoi(match[2]);
const std::string parsed_step = match[4];
const int sign = signum(end - start);
if (parsed_step.length() > 0) {
int step = std::stoi(parsed_step) * sign;
return new SameInterval(start, end, step);
} else {
return new SameInterval(start, end, sign);
}
}
static bool accept(const std::string &s) {
return std::regex_match(s, interval_regex);
}
};
std::regex SameInterval::interval_regex = std::regex(R"((-?\d+)-(-?\d+)(/(\d+))?)");
class Identical : public Inflatable {
private:
static std::regex identical_regex;
const int _value;
const size_t _count;
Identical(const int value, const int count) : _value(value), _count(count) {}
public:
[[nodiscard]] std::vector<int> inflate() const override {
std::vector<int> inflated;
for (size_t i = 0; i < _count; i++) {
inflated.push_back(_value);
}
return inflated;
}
static Inflatable *create_from(const std::string &s) {
std::vector<std::string> value_count = split(s, "*");
std::smatch match;
if (!std::regex_search(s, match, identical_regex)) {
throw std::exception();
}
const int value = std::stoi(match[1]);
const int count = std::stoi(match[2]);
return new Identical(value, count);
}
static bool accept(const std::string &s) {
return std::regex_match(s, identical_regex);
}
};
std::regex Identical::identical_regex = std::regex(R"((-?\d+)[*](\d+))");
class Simple : public Inflatable {
private:
const int _value;
explicit Simple(const int value) : _value(value) {}
public:
[[nodiscard]] std::vector<int> inflate() const override {
return std::vector<int>{_value};
}
static Inflatable *create_from(const std::string &s) {
const int value = std::stoi(s);
return new Simple(value);
}
};
Inflatable *Inflatable::create_from(const std::string &s) {
if (SameInterval::accept(s)) {
return SameInterval::create_from(s);
}
if (Identical::accept(s)) {
return Identical::create_from(s);
}
return Simple::create_from(s);
}
std::vector<std::string> split(const std::string &s, const std::string &delimiter) {
std::vector<std::string> substrings;
size_t start = 0;
size_t end;
std::string substring;
while ((end = s.find(delimiter, start)) != std::string::npos) {
size_t count = end - start;
substring = s.substr(start, count);
substrings.push_back(substring);
start = end + delimiter.length();
}
end = std::min(end, s.length());
substring = s.substr(start, end);
substrings.push_back(substring);
return substrings;
}