-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_10b.cpp
74 lines (70 loc) · 2 KB
/
day_10b.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
66
67
68
69
70
71
72
73
74
#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;
for (const auto c : s) {
lengths.emplace_back(int(c));
}
lengths.emplace_back(17);
lengths.emplace_back(31);
lengths.emplace_back(73);
lengths.emplace_back(47);
lengths.emplace_back(23);
return lengths;
}
void print(std::vector<int>& sparse_hash) {
for (const auto& ele : sparse_hash) {
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> sparse_hash(n);
for (int i = 0; i < n; i++) {
sparse_hash[i] = i;
}
const auto lengths = split_input_into_lengths(line);
for (int round = 0; round < 64; round++) {
for (const auto& length : lengths) {
for (int i = 0; i < length/2; i++) {
std::swap(sparse_hash[(current_position + i + n) % n], sparse_hash[(current_position + length - i + n - 1) % n]);
}
current_position += length + skip_size;
current_position %= n;
skip_size++;
skip_size %= n;
}
}
// print(sparse_hash);
std::vector<int> dense_hash;
for (int i = 0; i < n/ 16;i++) {
dense_hash.emplace_back(sparse_hash[i*16]);
for (int j = 1; j < 16; j++) {
dense_hash[i] = dense_hash[i] ^ sparse_hash[i*16 + j];
}
}
std::string result;
for (const auto& ele : dense_hash) {
std::stringstream sstream;
sstream << std::hex << ele;
std::string temp = sstream.str();
result += (temp.size() < 2) ? "0" + temp : temp;
}
std::cout << result << '\n';
return 0;
}