-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle_06_1.cc
55 lines (49 loc) · 1.19 KB
/
puzzle_06_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
#include <algorithm>
#include <boost/functional/hash.hpp>
#include <iostream>
#include <sstream>
#include <unordered_set>
#include <vector>
size_t checksum(const std::vector<int>& nums) {
size_t seed = 0;
for (auto n : nums) {
boost::hash_combine(seed, n);
}
return seed;
}
int main() {
std::vector<int> nums;
{
std::string line;
std::getline(std::cin, line);
std::stringstream ss{line};
while (!ss.eof()) {
int x;
ss >> x;
nums.push_back(x);
}
}
std::unordered_set<size_t> hashes;
hashes.insert(checksum(nums));
int steps = 0;
for(;;) {
auto it = std::max_element(std::begin(nums), std::end(nums));
if (it == std::end(nums)) {
std::cerr << "NO MAX!\n";
exit(1);
}
int val = 0;
std::swap(val, *it);
for(++it; val > 0; ++it, --val) {
if (it == std::end(nums)) {
it = std::begin(nums);
}
(*it)++;
}
++steps;
if (!hashes.insert(checksum(nums)).second) {
break;
}
}
std::cout << "steps: " << steps << "\n";
}