-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_16.1.cpp
61 lines (57 loc) · 1.69 KB
/
day_16.1.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
// Advent of Code 2017
// Day 16 - Permutation Promenade
#include <iostream>
#include <sstream>
#include <algorithm>
#include <fstream>
#include <map>
using namespace std;
int main()
{
// test inputs
/* string input = "s1, x3/4, pe/b"; // a spin of size 1: eabcd.*/
int n,n1,n2;
string ins;
char ch;
map<string,uint> seq; // cycle?
string p = "abcdefghijklmnop";
for(int i=0;i<1000000000%60;++i)
{
ifstream in("day_16.txt");
while(in >> ch) {
if (ch==',' || ch<'0' || ch=='/' || ch==' ')
continue;
if (ch=='s') {
in >> n;
string temp(p);
copy(end(p)-n,end(p),temp.begin());
copy(begin(p),end(p)-n,temp.begin()+n);
p=temp;
}
if (ch=='x') {
in >> n1;
if(in.peek()=='/') in.get();
in >> n2;
swap(p[n1],p[n2]);
}
if (ch=='p') {
string sw;
char ch;
while (in.peek()!=',' && in >> ch)
sw.push_back(ch);
auto slash = find_if(sw.begin(),sw.end(),[](int c)->bool{return c=='/';});
string t1(sw.begin(),slash);
string t2(slash+1,sw.end());
auto it1 = p.find(t1);
auto it2 = p.find(t2);
copy(t1.begin(),t1.end(),p.begin()+it2);
copy(t2.begin(),t2.end(),p.begin()+it1);
}
}
if (seq.find(p)!=seq.end()) {
cout << "Found a repeat of " << p << " at " << i << endl;
}
seq[p] = i;
}
cout << p << endl;
}