-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_05a.cpp
44 lines (41 loc) · 1.04 KB
/
day_05a.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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
int main(int argc, char* argv[]) {
std::string input = "../input/day_05_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string polymer;
std:getline(file, polymer);
const size_t delta1 = int('a') - int('A');
const size_t delta2 = int('A') - int('a');
std::size_t start = 0;
std::size_t end = 1;
while(end < polymer.size()) {
if (polymer[start] - polymer[end] == delta1 || polymer[start] - polymer[end] == delta2) {
polymer[start] = ' ';
polymer[end] = ' ';
while (polymer[start] == ' ' && start > 0) {
start--;
}
} else {
start += 1;
}
while (polymer[start] == ' ') {
start++;
}
end = start + 1;
while (polymer[end] == ' ') {
end++;
}
}
auto it = std::remove(std::begin(polymer), std::end(polymer), ' ');
polymer.erase(it, std::end(polymer));
const auto ans = polymer.size();
std::cout << ans << '\n';
return ans;
}