-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday21.cpp
257 lines (230 loc) · 8.02 KB
/
day21.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <fstream>
#include <iostream>
#include <regex>
#include <string>
#include <vector>
// https://adventofcode.com/2016/day/21
//#define TEST
#ifdef TEST
const std::string InputFile{"day21-input-test.txt"};
const std::string Passphrase{"abcde"};
const std::string Scrambled{"decab"};
#else
const std::string InputFile{"day21-input.txt"};
const std::string Passphrase{"abcdefgh"};
const std::string Scrambled{"fbgdceah"};
#endif
const std::string TwoInts{R"(.*\s(\d+)\s.*\s(\d+).*)"};
const std::string OneInt{R"(.*\s(\d+).*)"};
const std::string TwoChars{R"(.*\s([a-z])\s.*\s([a-z]).*)"};
const std::string OneChar{R"(.*\s([a-z]).*)"};
const std::regex TwoInts_re{TwoInts};
const std::regex OneInt_re{OneInt};
const std::regex TwoChars_re{TwoChars};
const std::regex OneChar_re{OneChar};
enum CommandType {
SwapPosition,
SwapLetter,
ReversePositions,
RotateLeft,
RotateRight,
Rotate,
MovePosition,
};
struct Command {
CommandType cmd;
int n1, n2;
char c1, c2;
};
void swap_position(std::vector<char>& pwd, int p1, int p2) {
char tmp = pwd[p1];
pwd[p1] = pwd[p2];
pwd[p2] = tmp;
}
void swap_letter(std::vector<char>& pwd, char c1, char c2) {
int p1 = -1, p2 = -1;
for (int i = 0; p1 < 0 || p2 < 0; i++) {
if (pwd[i] == c1) {
p1 = i;
} else if (pwd[i] == c2) {
p2 = i;
}
}
char tmp = pwd[p1];
pwd[p1] = pwd[p2];
pwd[p2] = tmp;
}
void reverse_positions(std::vector<char>& pwd, int p1, int p2) {
std::vector<char> swap;
for (int i = p1; i <= p2; i++) {
swap.push_back(pwd[i]);
}
for (int i = 0; i < swap.size(); i++) {
pwd[p2 - i] = swap[i];
}
}
void rotate(std::vector<char>& pwd, int steps) {
int sz = pwd.size();
std::vector<char> result(sz, ' ');
for (int i = 0; i < sz; i++) {
int newi = ((((i + steps) % sz) + sz) % sz);
result[newi] = pwd[i];
}
for (int i = 0; i < sz; i++) {
pwd[i] = result[i];
}
}
void rotate_left(std::vector<char>& pwd, int steps) {
rotate(pwd, -steps);
}
void rotate_right(std::vector<char>& pwd, int steps) {
rotate(pwd, steps);
}
void rotate_based_on(std::vector<char>& pwd, char c1) {
int p1 = -1;
for (int i = 0; p1 < 0; i++) {
if (pwd[i] == c1) {
p1 = i;
}
}
int steps = p1 >= 4 ? 2 + p1 : 1 + p1;
rotate_right(pwd, steps);
}
void unrotate_based_on(std::vector<char>& pwd, char c1) {
// Keep rotating left until it matches the equivalent rotate_based_on result
// Could (Should?) really do this by working out how many left rotations
// based on the index of c1 but that doesn't really work well/easily in the
// general case. This brute force / iterative moethod works fine for smallish
// lengths of password.
std::vector<char> tmp1{pwd};
int rcount = 0;
while (true) {
rcount++;
rotate_left(tmp1, 1);
std::vector<char> tmp2{tmp1};
rotate_based_on(tmp2, c1);
if (tmp2 == pwd) {
rotate_left(pwd, rcount);
break;
}
}
}
void move_position(std::vector<char>& pwd, int p1, int p2) {
int sz = pwd.size();
std::vector<char> result(sz, ' ');
for (int i = 0; i < sz; i++) {
int newi = i;
if ((i < p1 && i < p2) || (i > p1 && i > p2)) {
result[i] = pwd[i];
} else if (i == p2) {
result[i] = pwd[p1];
} else if (i >= p1 && i < p2) {
result[i] = pwd[i+1];
} else if (i <= p1 && i > p2) {
result[i] = pwd[i-1];
}
}
for (int i = 0; i < sz; i++) {
pwd[i] = result[i];
}
}
int main() {
std::fstream inputfile(InputFile, std::ios_base::in | std::ios_base::binary);
std::string line;
std::smatch sm;
std::vector<Command> commands;
while (std::getline(inputfile, line)) {
// Strip CR
line.erase(remove(line.begin(), line.end(), '\r'), line.end());
if (line.rfind("swap position", 0) == 0) {
std::regex_match(line, sm, TwoInts_re);
commands.push_back(Command{CommandType::SwapPosition, std::stoi(sm[1]), std::stoi(sm[2]), ' ', ' '});
} else if (line.rfind("swap letter", 0) == 0) {
std::regex_match(line, sm, TwoChars_re);
commands.push_back(Command{CommandType::SwapLetter, 0, 0, *sm[1].first, *sm[2].first});
} else if (line.rfind("reverse positions", 0) == 0) {
std::regex_match(line, sm, TwoInts_re);
commands.push_back(Command{CommandType::ReversePositions, std::stoi(sm[1]), std::stoi(sm[2]), ' ', ' '});
} else if (line.rfind("rotate based on", 0) == 0) {
std::regex_match(line, sm, OneChar_re);
commands.push_back(Command{CommandType::Rotate, 0, 0, *sm[1].first, ' '});
} else if (line.rfind("rotate left", 0) == 0) {
std::regex_match(line, sm, OneInt_re);
commands.push_back(Command{CommandType::RotateLeft, std::stoi(sm[1]), 0, ' ', ' '});
} else if (line.rfind("rotate right", 0) == 0) {
std::regex_match(line, sm, OneInt_re);
commands.push_back(Command{CommandType::RotateRight, std::stoi(sm[1]), 0, ' ', ' '});
} else if (line.rfind("move position", 0) == 0) {
std::regex_match(line, sm, TwoInts_re);
commands.push_back(Command{CommandType::MovePosition, std::stoi(sm[1]), std::stoi(sm[2]), ' ', ' '});
} else {
std::cout << "Unknown scrambler instruction: " << line << std::endl;
return -1;
}
}
std::vector<char> passphrase;
for (auto &&c : Passphrase) {
passphrase.push_back(c);
}
for (int i = 0; i < commands.size(); i++) {
Command cmd = commands[i];
switch (cmd.cmd) {
case CommandType::SwapPosition:
swap_position(passphrase, cmd.n1, cmd.n2);
break;
case CommandType::SwapLetter:
swap_letter(passphrase, cmd.c1, cmd.c2);
break;
case CommandType::ReversePositions:
reverse_positions(passphrase, cmd.n1, cmd.n2);
break;
case CommandType::RotateLeft:
rotate_left(passphrase, cmd.n1);
break;
case CommandType::RotateRight:
rotate_right(passphrase, cmd.n1);
break;
case CommandType::MovePosition:
move_position(passphrase, cmd.n1, cmd.n2);
break;
case CommandType::Rotate:
rotate_based_on(passphrase, cmd.c1);
break;
}
}
std::string part1(passphrase.begin(), passphrase.end());
std::cout << "Part 1: " << part1 << std::endl;
std::vector<char> scrambled;
//for (auto &&c : part1) {
for (auto &&c : Scrambled) {
scrambled.push_back(c);
}
for (int i = commands.size() - 1; i >= 0; i--) {
Command cmd = commands[i];
switch (cmd.cmd) {
case CommandType::SwapPosition: // Same
swap_position(scrambled, cmd.n1, cmd.n2);
break;
case CommandType::SwapLetter: // Same
swap_letter(scrambled, cmd.c1, cmd.c2);
break;
case CommandType::ReversePositions: // Same
reverse_positions(scrambled, cmd.n1, cmd.n2);
break;
case CommandType::RotateLeft: // Opposite rotate
rotate_right(scrambled, cmd.n1);
break;
case CommandType::RotateRight: // Opposite rotate
rotate_left(scrambled, cmd.n1);
break;
case CommandType::MovePosition: // Swap the move position parameters
move_position(scrambled, cmd.n2, cmd.n1);
break;
case CommandType::Rotate: // Un-rotate
unrotate_based_on(scrambled, cmd.c1);
break;
}
}
std::string part2(scrambled.begin(), scrambled.end());
std::cout << "Part 2: " << part2 << std::endl;
}