-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaySeventeen.cpp
More file actions
236 lines (218 loc) · 6.55 KB
/
Copy pathdaySeventeen.cpp
File metadata and controls
236 lines (218 loc) · 6.55 KB
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
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <bitset>
// Literal Operand Land // Combo Operand Cand
namespace DaySeventeen
{
namespace {
using llong_t = long long;
using instruction_set_t = std::vector<std::pair<llong_t, llong_t>>;
void coutState();
void coutCreateInstructionSet();
enum class OpCode : llong_t {
Adv = 0, // A / Cand^2 truncated to int -> A
Bxl = 1, // bitwise XOR B & Land -> B
Bst = 2, // Cabd % 8 (aka only last three bits) -> B
Jnz = 3, // nothing if A=0; else intstruction pointer jumps by Lan instead of two (check evenness)
Bxc = 4, // botwise XOR B & C -> B, operand ignored
Out = 5, // Cand % 8 -> output
Bdv = 6, // A / Cand^2 truncated to int -> B
Cdv = 7 // A / Cand^2 truncated to int -> C
};
const bool toLog = false; // true false
const llong_t kMaxIter = 1'000'00;
#pragma region defs
llong_t regA = 0;
llong_t regB = 0;
llong_t regC = 0;
instruction_set_t instructionSet;
size_t instructionIndex = 0;
std::string output = "";
#pragma endregion
llong_t getComboOperand(llong_t operand)
{
switch (operand) {
case 0:
case 1:
case 2:
case 3:
return operand;
case 4:
return regA;
case 5:
return regB;
case 6:
return regC;
default:
std::cout << "\nInvalid combo operand value:" << operand << '\n';
throw std::runtime_error("Invalid combo operand value");
}
}
void executeInstruction(std::pair<llong_t, llong_t> instruction)
{
if (instruction.first < static_cast<llong_t>(OpCode::Adv) || // best comparison to a < .size() call for cpp enum
instruction.first > static_cast<llong_t>(OpCode::Cdv)) {
throw std::runtime_error("Invalid OpCode value");
}
OpCode opCode = static_cast<OpCode>(instruction.first);
llong_t operand = instruction.second;
if (toLog) std::cout << "Operating: {" << instruction.first << ", " << instruction.second << "}\n";
switch (opCode) {
case OpCode::Adv: {
llong_t denA = 1 << getComboOperand(operand); // more efficient 2^int operation through left bitshift
if (denA == 0) {
throw std::runtime_error("Division by zero in Adv instruction");
}
regA = regA / denA;
break;
}
case OpCode::Bxl:
regB = regB ^ operand; // bitwise XOR doesn't care about leading false bits so default int is fine
break;
case OpCode::Bst:
regB = getComboOperand(operand) % 8;
break;
case OpCode::Jnz:
if (regA != 0) {
if (operand % 2 != 0) {
std::cout << "\nOperand for Jnz must be even:" << operand << '\n';
throw std::runtime_error("Operand for Jnz must be even");
}
instructionIndex = (static_cast<size_t>(operand / 2)) - 1; // can underflow, will immediately overflow in the fori
break;
}
case OpCode::Bxc:
regB = regB ^ regC;
break;
case OpCode::Out: {
llong_t result = getComboOperand(operand) % 8;
output += std::to_string(result) + ","; // can only be single digit
break;
}
case OpCode::Bdv: {
llong_t denominator = 1 << getComboOperand(operand); // more efficient 2^int operation through left bitshift
if (denominator == 0) {
throw std::runtime_error("Division by zero in Adv instruction");
}
regB = regA / denominator;
break;
}
case OpCode::Cdv: {
llong_t denominator = 1 << getComboOperand(operand); // more efficient 2^int operation through left bitshift
if (denominator == 0) {
throw std::runtime_error("Division by zero in Adv instruction");
}
regC = regA / denominator;
break;
}
default:
throw std::runtime_error("Unhandled OpCode");
}
if (toLog) coutState();
}
void parseInputLine(llong_t i, std::string line)
{
switch (i) {
case 1:
regA = std::stoll(line.substr(line.find(":") + 1));
return;
case 2:
regB = std::stoll(line.substr(line.find(":") + 1));
return;
case 3:
regC = std::stoll(line.substr(line.find(":") + 1));
return;
case 4:
return;
case 5: {
instructionSet.clear();
size_t startPos = line.find(" ") + 1;
line = line.substr(startPos);
size_t pos = 0;
while ((pos = line.find(",")) != std::string::npos) {
llong_t opCode = std::stoi(line.substr(0, pos));
line.erase(0, pos + 1);
pos = line.find(",");
llong_t operand = std::stoi(line.substr(0, pos));
line.erase(0, pos + 1);
instructionSet.emplace_back(opCode, operand);
}
return;
}
default:
std::cout << '\n' << line << '\n';
throw std::runtime_error("line parsing error");
}
}
void handleFile(std::ifstream& inputFile)
{
if (inputFile.is_open()) {
std::string line;
llong_t i = 1;
llong_t startingRegA = 0;
while (getline(inputFile, line)) {
parseInputLine(i, line);
if (i == 1) {
startingRegA = regA;
}
++i;
}
std::cout << "Finished reading file\n";
coutCreateInstructionSet();
coutState();
inputFile.close(); // automatically happens when going out of scope but no longer needed. More about explicitness.
std::cout << "Executing Assembly:\n";
size_t s = instructionSet.size();
for (llong_t offset = 0; offset < 100; ++offset) {
regA = startingRegA + offset;
instructionIndex = 0;
output.clear();
std::cout << "Testing regA = " << regA << " (Binary: " << std::bitset<64>(regA) << ")\n";
for (; instructionIndex < s; ++instructionIndex) {
executeInstruction(instructionSet[instructionIndex]);
}
std::cout << "Output for regA = " << regA << ": " << output << '\n';
}
std::cout << "Output: " << output << '\n';
std::cout << "\nFinished running program";
}
else {
std::cout << "Unable to open file\n";
}
}
void coutState()
{
std::cout << "Register A: " << regA
<< ", Register B: " << regB
<< ", Register C: " << regC
<< ", Output: " << output << '\n'
<< "Bits: " << std::bitset<32>(regA) << '\n';
}
void coutCreateInstructionSet()
{
for (const auto& instruction : instructionSet) {
std::cout << "{" << instruction.first
<< ", " << instruction.second << "}, ";
}
std::cout << '\n';
}
}
void daySeventeen() {
std::system("cls"); // clear terminal pre-boot
std::cout << "Running program DaySeventeen" << '\n';
const bool isTestFile = false; // true false
std::string line;
std::ifstream inputFile;
if (isTestFile) {
std::cout << "Using test file\n\n";
}
else {
std::cout << "Using full file\n\n";
}
(isTestFile) ? inputFile.open("daySeventeenTest.txt") : inputFile.open("daySeventeenFull.txt");
handleFile(inputFile);
}
}