-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_10a.cpp
63 lines (58 loc) · 2.06 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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <limits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <regex>
std::vector<int> split_input_into_lengths(const std::string& s) {
std::vector<int> lengths;
std::size_t start = 0;
const std::string delimiter = ",";
std::size_t end = s.find(delimiter);
while(end != std::string::npos) {
lengths.emplace_back(std::stoi(s.substr(start, end - start)));
start = end + delimiter.size();
end = s.find(delimiter, start);
}
lengths.emplace_back(std::stoi(s.substr(start, end - start)));
return lengths;
}
void print(std::vector<int>& hash_str) {
for (const auto& ele : hash_str) {
std::cout << ele << ' ';
}
std::cout << '\n';
}
int main(int argc, char* argv[]) {
const std::string input = (argc > 1) ? argv[1] : "../input/day_10_input" ;
std::ifstream file(input);
std::string line;
constexpr int n = 256; // This will need to change to run with the sample input
std::getline(file, line);
int current_position = 0;
int prev_position = n - 1;
int skip_size = 0;
std::vector<int> hash_str(n);
for (int i = 0; i < n; i++) {
hash_str[i] = i;
}
// print(hash_str);
const auto lengths = split_input_into_lengths(line);
for (const auto& length : lengths) {
// std::cout << "length: " << length << '\n';
for (int i = 0; i < length/2; i++) {
// std::cout << "swapping positions: " << (current_position + i + n) % n << " and " << (current_position + length - i + n - 1) % n << '\n';
// std::cout << "swapping values: " << hash_str[(current_position + i + n) % n] << " and " << hash_str[(current_position + length - i + n - 1) % n] << '\n';
std::swap(hash_str[(current_position + i + n) % n], hash_str[(current_position + length - i + n - 1) % n]);
}
// print(hash_str);
current_position += length + skip_size;
skip_size++;
}
std::cout << hash_str[0] * hash_str[1] << '\n';
// std::cout << hash_str[current_position % n] * hash_str[(current_position + 1) % n] << '\n';
return 0;
}