-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle_14_1.cc
63 lines (55 loc) · 1.56 KB
/
puzzle_14_1.cc
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 <array>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
const unsigned NUMS = 256;
int main() {
int sum = 0;
std::string line;
while (std::getline(std::cin, line)) {
std::vector<unsigned> input;
for (const unsigned& c : line) {
input.push_back(c);
}
input.push_back(17);
input.push_back(31);
input.push_back(73);
input.push_back(47);
input.push_back(23);
std::array<unsigned char, NUMS> nums;
for (unsigned i = 0; i < NUMS; ++i) {
nums[i] = i;
}
int skip = 0;
int pos = 0;
for (unsigned round = 0; round < 64; ++round) {
for (auto w : input) {
int from = pos, to = (pos + w - 1) % NUMS;
for (unsigned i = w / 2; i > 0; --i) {
std::swap(nums[from], nums[to]);
from = ++from % NUMS;
if (to == 0) {
to = NUMS;
}
to -= 1;
}
pos = (pos + w + skip++) % NUMS;
}
}
for (int i = 0; i < 16; ++i) {
int from = 16 * i;
int to = 16 * (i + 1);
unsigned char h = nums[from];
for (++from; from < to; ++from) {
h ^= nums[from];
}
for(int b = 0; b < 8; ++b) {
sum += (h & 0x1);
h >>= 1;
}
}
}
std::cout << "sum: " << sum << "\n";
return 0;
}