-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_14a.cpp
96 lines (90 loc) · 2.65 KB
/
day_14a.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
#include <limits>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <regex>
#include <bitset>
#include <climits>
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';
}
std::string create_hash(const std::string& line) {
// std::cout << '|' << line << '|'<<'\n';
constexpr int n = 256; // This will need to change to run with the sample input
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;
}
}
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;
std::bitset<128> result_bin;
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;
}
for (const auto c : result) {
result_bin <<= 4;
// std::cout << c << '\n';
if (c >= '0' && c <= '9') {
result_bin |= std::bitset<128>(c - '0');
} else {
result_bin |= std::bitset<128>(c - 'a' + 10);
}
}
return result_bin.to_string();
}
int main(int argc, char* argv[]) {
const std::string input = (argc > 1) ? argv[1] : "../input/day_14_input" ;
std::ifstream file(input);
std::string line;
std::getline(file, line);
int used_bits = 0;
for (int i = 0; i < 128; i++) {
const auto grid_row = create_hash(line + "-" + std::to_string(i));
used_bits += std::count_if(std::begin(grid_row), std::end(grid_row), [](const auto ele) {return ele == '1';});
}
std::cout << used_bits << '\n';
return 0;
}