-
Notifications
You must be signed in to change notification settings - Fork 0
/
main02.cpp
73 lines (60 loc) · 1.97 KB
/
main02.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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
void runIntCode(std::vector<int> & code) {
for (size_t i = 0; i < code.size(); i+=4) {
int opcode = code[i];
int param1 = code[i+1];
int param2 = code[i+2];
int param3 = code[i+3];
if (opcode == 1) // ALTERNATIVE: Expression templates
code[param3] = code[param1] + code[param2];
else if (opcode == 2)
code[param3] = code[param1] * code[param2];
else if (opcode == 99)
break;
else {
std::cout << "ERROR: something went wrong - opcode = " << opcode;
std::cout << " (opcode must be 1, 2 or 99)\n";
}
}
}
void findInput(const std::vector<int> & initCode,
const int & desiredOut, std::vector<int> & input)
{
for (int noun = 0; noun < 99; noun++) {
for (int verb = 0; verb < 99; verb++) {
std::vector<int> modCode(initCode);
modCode[1] = noun;
modCode[2] = verb;
runIntCode(modCode);
if (modCode[0] == desiredOut) {
input.push_back(noun);
input.push_back(verb);
std::cout << "\n - - - PART 2 - - - \n";
std::cout << "OUTPUT: " << modCode[0];
std::cout << " with input (solution) " << 100*noun+verb << "\n";
break;
}
if (noun == 12 && verb == 2) {
std::cout << "\n - - - PART 1 - - - \n";
std::cout << "Value at position 0: " << modCode[0] << "\n";
}
}
}
}
// ----- MAIN -----
int main() {
std::ifstream inFile("./input_files/in02.txt");
std::vector<int> initCode;
std::string val;
if (inFile.is_open()) {
while (getline(inFile, val, ',')) {
initCode.push_back(std::stoi(val));
}
}
int desiredOut = 19690720;
std::vector<int> input;
findInput(initCode, desiredOut, input);
}