-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_10a.cpp
65 lines (61 loc) · 1.77 KB
/
day_10a.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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
std::vector<int> flatten_sequence (const std::vector<std::pair<int, int>>& sequence) {
std::vector<int> flatten;
for (const auto& [i,j] : sequence) {
flatten.push_back(i);
flatten.push_back(j);
}
// std::cout << "flatten: ";
// for (const auto f : flatten) {
// std::cout << f << ' ';
// }
// std::cout << '\n';
return flatten;
}
std::vector<std::pair<int, int>> create_sequence(const std::vector<int>& flatten) {
std::vector<std::pair<int, int>> sequence;
int i = 0;
int count = 1;
while (i < flatten.size() - 1) {
if (flatten[i] == flatten[i+1]) {
count++;
} else {
// std::cout << "Pushing " << count << "," << flatten[i] << '\n';
sequence.push_back({count, flatten[i]});
count = 1;
}
i++;
}
sequence.push_back({count, flatten.back()});
// for (const auto& [i,j] : sequence) {
// std::cout << i << ' ' << j << ' ';
// }
// std::cout << '\n';
return sequence;
}
int main(int argc, char* argv[]) {
std::string input = "../input/day_10_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string line;
std::getline(file, line);
std::vector<int> flatten;
for (const auto c : line) {
flatten.push_back(c - '0');
}
std::vector<std::pair<int, int>> sequence = create_sequence(flatten);
for (int iter = 0; iter < (40 - 1); iter++) {
flatten = flatten_sequence(sequence);
sequence = create_sequence(flatten);
}
flatten = flatten_sequence(sequence);
std::cout << flatten.size() << '\n';
return 0;
}