-
Notifications
You must be signed in to change notification settings - Fork 2
/
21.cpp
158 lines (140 loc) · 4.59 KB
/
21.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
#include <algorithm>
#include <chrono>
#include <iostream>
#include <string>
#include <vector>
enum OpCode { SWAP_POS, SWAP_CHAR, ROTATE, ROTATE_ON_CHAR_POS, REVERSE, MOVE };
struct Instruction {
OpCode op;
unsigned char a;
unsigned char b;
};
using std::string, std::string_view;
using std::vector;
using size_type = string::size_type;
string scramble(string s, const vector<Instruction>& instructions) {
char tmp;
for (const Instruction& ins : instructions) {
switch (ins.op) {
case SWAP_POS: {
tmp = s[ins.a];
s[ins.a] = s[ins.b];
s[ins.b] = tmp;
break;
}
case SWAP_CHAR: {
size_type a = s.find(static_cast<char>(ins.a));
size_type b = s.find(static_cast<char>(ins.b));
s[a] = static_cast<char>(ins.b);
s[b] = static_cast<char>(ins.a);
break;
}
case ROTATE: {
string c = s;
int len = static_cast<int>(s.length());
for (int i = 0; i < len; i++) {
int pos = i + static_cast<int>(ins.a);
while (pos < 0) {
pos += len;
}
while (pos >= len) {
pos -= len;
}
s[static_cast<unsigned int>(i)] = c[static_cast<unsigned int>(pos)];
}
break;
}
case ROTATE_ON_CHAR_POS: {
int n = static_cast<int>(s.find(static_cast<char>(ins.a)));
n += (n >= 4) ? 2 : 1;
n *= -1;
string c = s;
int len = static_cast<int>(s.length());
for (int i = 0; i < len; i++) {
int pos = i + n;
while (pos < 0) {
pos += len;
}
s[static_cast<size_type>(i)] = c.at(static_cast<size_type>(pos));
}
break;
}
case REVERSE: {
string c = s;
for (unsigned int i = ins.a, j = ins.b; i <= ins.b; i++, j--) {
s[i] = c[j];
}
break;
}
case MOVE: {
char c = s.at(ins.a);
// shift everything to the right of ins.a to the left
for (size_type i = ins.a; i < s.length(); i++) {
s[i] = s[i + 1];
}
// insert c at ins.b
for (size_type i = s.length() - 1; i > ins.b; i--) {
s[i] = s[i - 1];
}
s[ins.b] = c;
break;
}
}
}
return s;
}
vector<Instruction> parse_input() {
string input;
vector<Instruction> ins;
while (std::getline(std::cin, input)) {
if (input.find("swap position") != std::string::npos) {
ins.push_back(Instruction{SWAP_POS,
static_cast<unsigned char>(input[14] - '0'),
static_cast<unsigned char>(input[30] - '0')});
} else if (input.find("swap letter") != std::string::npos) {
ins.push_back(Instruction{SWAP_CHAR,
static_cast<unsigned char>(input[12]),
static_cast<unsigned char>(input[26])});
} else if (input.find("rotate based") != std::string::npos) {
ins.push_back(Instruction{ROTATE_ON_CHAR_POS,
static_cast<unsigned char>(input[35]), 0});
} else if (input.find("rotate") != std::string::npos) {
ins.push_back(Instruction{
ROTATE,
static_cast<unsigned char>(input[7] == 'r' ? -(input[13] - '0')
: input[12] - '0'),
0});
} else if (input.find("reverse") != std::string::npos) {
ins.push_back(Instruction{REVERSE,
static_cast<unsigned char>(input[18] - '0'),
static_cast<unsigned char>(input[28] - '0')});
} else {
ins.push_back(Instruction{MOVE,
static_cast<unsigned char>(input[14] - '0'),
static_cast<unsigned char>(input[28] - '0')});
}
}
return ins;
}
int main() {
auto tstart = std::chrono::high_resolution_clock::now();
vector<Instruction> ins = parse_input();
string pt1 = scramble("abcdefgh", ins);
// lazy pt2, just permute all 8**8 options
string pt2 = "abcdefgh";
while (std::next_permutation(pt2.begin(), pt2.end()) != false) {
if (scramble(pt2, ins) == "fbgdceah") {
break;
}
}
std::cout << "--- Day 21: Scrambled Letters and Hash ---\n";
std::cout << "Part 1: " << pt1 << "\n";
std::cout << "Part 2: " << pt2 << "\n";
auto tstop = std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(tstop - tstart);
std::cout << "Time: " << duration.count() << " μs"
<< "\n";
return EXIT_SUCCESS;
}
// bedhafcg not right