-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_02a.cpp
47 lines (44 loc) · 1.26 KB
/
day_02a.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
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
int main(int argc, char *argv[]) {
// Get input
std::string input = "../input/day_02_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string input_str;
const std::string delimiter = ",";
std::getline(file, input_str);
std::size_t start = 0;
std::size_t end = input_str.find(delimiter);
std::vector<int> program;
while (end != std::string::npos) {
program.emplace_back(std::stoi(input_str.substr(start, end - start)));
start = end + delimiter.size();
end = input_str.find(delimiter, start);
}
program.emplace_back(std::stoi(input_str.substr(start, end - start)));
// Modify input according to puzzle instructions
program[1] = 12;
program[2] = 2;
// Solve
size_t index = 0;
while (program[index] != 99) {
if (program[index] == 1) {
program[program[index + 3]] =
program[program[index + 1]] + program[program[index + 2]];
} else if (program[index] == 2) {
program[program[index + 3]] =
program[program[index + 1]] * program[program[index + 2]];
} else {
std::cout << "Error" << '\n';
}
index += 4;
}
std::cout << program[0] << '\n';
return program[0];
}