-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.cpp
165 lines (136 loc) · 4.64 KB
/
part1.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "Day21.hpp"
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
using namespace std;
Result Day21::solve_p1(){
string password = "abcdefgh";
//string password = "abcde";
return {true, scramble_pw(password)};
}
string Day21::scramble_pw(string &pw){
string password = pw;
// iterate over input lines
for(string line: this->data){
if( line.find("swap position") != std::string::npos ){
stringstream instr(line.substr(13));
int idx_start, idx_end;
string with, position;
instr >> idx_start >> with >> position >> idx_end;
char tmp = password[idx_start];
password[idx_start] = password[idx_end];
password[idx_end] = tmp;
} else if( line.find("swap letter") != std::string::npos ){
stringstream instr(line.substr(11));
char ch1, ch2;
string with, letter;
instr >> ch1 >> with >> letter >> ch2;
for(unsigned ii = 0; ii < password.length(); ++ii){
if( password[ii] == ch1 ){
password[ii] = ch2;
} else if( password[ii] == ch2 ){
password[ii] = ch1;
}
}
} else if( line.find("rotate ") != std::string::npos ){
stringstream instr(line.substr(7));
string direction;
instr >> direction;
if( direction == "left" || direction == "right" ){
int steps;
instr >> steps;
if( steps == 0){
continue;
}
string pw2 = "";
unsigned idx = 0;
if( direction == "right" ){
idx = password.length() - steps;
} else {
idx = steps;
}
while( pw2.length() != password.length() ){
pw2 += password[idx];
idx = (idx + 1) % password.length();
}
password = pw2;
} else if( direction == "based" ){
string on, position, of, letter;
char ch;
instr >> on >> position >> of >> letter >> ch;
int ii = 0;
while( password[ii] != ch ){
++ii;
}
++ii;
if( ii >= 5 ){
++ii;
}
ii %= password.length();
if( ii == 0){
continue;
}
int idx = password.length() - ii;
string pw2 = "";
while( pw2.length() != password.length() ){
pw2 += password[idx];
idx = (idx + 1) % password.length();
}
password = pw2;
} else {
cerr << "ERROR!!!" << direction << endl;
}
} else if( line.find("reverse position") != std::string::npos ){
stringstream instr(line.substr(18));
unsigned idx_start, idx_end;
string through;
instr >> idx_start >> through >> idx_end;
string pw2 = "";
unsigned idx = 0;
while( idx < idx_start ){
pw2 += password[idx];
++idx;
}
string reversed = "";
while( idx <= idx_end ){
reversed += password[idx];
++idx;
}
std::reverse(reversed.begin(), reversed.end());
pw2 += reversed;
while(idx < password.length()){
pw2 += password[idx];
++idx;
}
password = pw2;
} else if( line.find("move position") != std::string::npos ){
stringstream instr(line.substr(14));
unsigned x, y = 0;
string to, position;
instr >> x >> to >> position >> y;
char tmp = password[x];
string pw2 = "";
unsigned idx1 = 0, idx2 = 0;
//cout << "\t";
while( pw2.length() < password.length() ){
if( idx2 == y ){
pw2 += tmp;
++idx2;
if( pw2.length() == password.length() ){
break;
}
}
if( idx1 == x ){
++idx1;
}
pw2 += password[idx1];
++idx2;
++idx1;
}
password = pw2;
}
}
// END iterate over input lines
return password;
}