-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_05b.cpp
108 lines (103 loc) · 3.01 KB
/
day_05b.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
#include <cctype>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
std::vector<int> getNOps(const size_t n, const size_t inst_ptr, int modes,
const std::vector<int>& program) {
std::vector<int> ops;
for (size_t i = 1; i <= n; ++i) {
size_t rem = modes % 10;
modes = modes / 10;
if (rem == 0) {
ops.push_back(program[program[inst_ptr + i]]);
} else {
ops.push_back(program[inst_ptr + i]);
}
}
return ops;
}
bool isNumber(const std::string& str) {
for (char const& c : str) {
if (std::isdigit(c) == 0) return false;
}
return true;
}
int main(int argc, char* argv[]) {
// Get input
std::string input = "../input/day_05_input";
size_t input_position = 1;
if (argc > 1 && !isNumber(std::string(argv[1]))) {
input = argv[1];
input_position += 1;
}
std::ifstream file(input);
std::string input_str;
std::getline(file, input_str);
const std::string delimiter = ",";
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)));
// Solve
size_t inst_ptr = 0;
int output = -1;
while (program[inst_ptr] != 99) {
const int instr = program[inst_ptr];
const int opcode = instr % 100;
const int modes = instr / 100;
if (opcode == 1) {
const auto ops = getNOps(2, inst_ptr, modes, program);
program[program[inst_ptr + 3]] = ops[0] + ops[1];
inst_ptr += 4;
} else if (opcode == 2) {
const auto ops = getNOps(2, inst_ptr, modes, program);
program[program[inst_ptr + 3]] = ops[0] * ops[1];
inst_ptr += 4;
} else if (opcode == 3) {
program[program[inst_ptr + 1]] = std::stoi(argv[input_position]);
inst_ptr += 2;
input_position += 1;
} else if (opcode == 4) {
output = program[program[inst_ptr + 1]];
inst_ptr += 2;
} else if (opcode == 5) {
const auto ops = getNOps(2, inst_ptr, modes, program);
if (ops[0] != 0) {
inst_ptr = ops[1];
} else {
inst_ptr += 3;
}
} else if (opcode == 6) {
const auto ops = getNOps(2, inst_ptr, modes, program);
if (ops[0] == 0) {
inst_ptr = ops[1];
} else {
inst_ptr += 3;
}
} else if (opcode == 7) {
const auto ops = getNOps(2, inst_ptr, modes, program);
if (ops[0] < ops[1]) {
program[program[inst_ptr + 3]] = 1;
} else {
program[program[inst_ptr + 3]] = 0;
}
inst_ptr += 4;
} else if (opcode == 8) {
const auto ops = getNOps(2, inst_ptr, modes, program);
if (ops[0] == ops[1]) {
program[program[inst_ptr + 3]] = 1;
} else {
program[program[inst_ptr + 3]] = 0;
}
inst_ptr += 4;
}
}
std::cout << output << '\n';
return output;
}