-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCalculateRotors.cpp
78 lines (60 loc) · 1.81 KB
/
CalculateRotors.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
#include <set>
#include "LifeAPI.h"
#include "Parsing.hpp"
#include "LifeStableState.hpp"
#include "RotorDescription.hpp"
std::pair<LifeState, unsigned> DeterminePeriod(const LifeState &state) {
LifeState current = state;
std::stack<std::pair<uint64_t, int>> minhashes;
for (unsigned i = 1; i < 1000; i++) {
uint64_t newhash = current.GetHash();
while(true) {
if(minhashes.empty())
break;
if(minhashes.top().first < newhash)
break;
if(minhashes.top().first == newhash) {
unsigned p = i - minhashes.top().second;
return {current, p};
}
if(minhashes.top().first > newhash)
minhashes.pop();
}
minhashes.push({newhash, i});
current.Step();
}
return {current, 0};
}
int main(int argc, char *argv[]) {
std::set<std::string> seenRotors;
std::string rotorfile = "knownrotors";
if (argc > 1)
rotorfile = argv[1];
for (auto r : ReadRotors(rotorfile))
seenRotors.insert(r);
std::string rle;
for (std::string line; std::getline(std::cin, line);) {
if(line[0] != 'x') {
rle.append(line);
rle.append("\n");
}
}
LifeState input = LifeState::Parse(rle.c_str());
auto [osc, period] = DeterminePeriod(input);
if (period == 0)
std::cout << "Not Oscillating?" << std::endl;
std::cout << "Oscillating! Period: " << period << std::endl;
LifeUnknownState unknownState {osc, LifeState(), LifeState()};
LifeStableState stable;
stable.SetOn(osc);
stable.SetOff(~osc);
for(auto &r : GetSeparatedRotorDesc(unknownState, stable, period)) {
auto rotorDesc = r.ToString();
if (seenRotors.contains(rotorDesc))
std::cout << "Known Rotor: " << rotorDesc << std::endl;
else {
seenRotors.insert(rotorDesc);
std::cout << "New Rotor: " << rotorDesc << std::endl;
}
}
}