-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_21a.cpp
96 lines (84 loc) · 3.73 KB
/
day_21a.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
#include <array>
#include <fstream>
#include <functional>
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
std::pair<std::string, std::array<int, 3>> parse_input(const std::string& s_main) {
std::string s = s_main;
std::array<int, 3> nums;
std::string delimiter = " ";
std::size_t start = 0;
auto end = s.find(delimiter);
const auto instr = s.substr(start, end - start);
start = end + 1;
end = s.find(delimiter, start);
int count = 0;
while (end != std::string::npos) {
nums[count] = std::stoi(s.substr(start, end - start));
start = end + delimiter.size();
end = s.find(delimiter, start);
count++;
}
nums[count] = std::stoi(s.substr(start, s.size() - start));
return {instr, nums};
}
template<typename T>
void print(const T& t) {
std::cout << "[";
for (const auto& ele : t) {
std::cout << ele << ' ';
}
std::cout << "]";
std::cout << '\n';
}
int main(int argc, char * argv []) {
std::string input = "../input/day_21_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string line;
std::array<unsigned long long, 6> registers;
const std::unordered_map<std::string, std::function<void(const int, const int, const int)>> map
{
{"addr", { [®isters](const int a, const int b, const int c) { registers[c] = registers[a] + registers[b]; } } },
{"addi", { [®isters](const int a, const int b, const int c) { registers[c] = registers[a] + b; } } },
{"mulr", { [®isters](const int a, const int b, const int c) { registers[c] = registers[a] * registers[b]; } } },
{"muli", { [®isters](const int a, const int b, const int c) { registers[c] = registers[a] * b; } } },
{"banr", { [®isters](const int a, const int b, const int c) { registers[c] = registers[a] & registers[b]; } } },
{"bani", { [®isters](const int a, const int b, const int c) { registers[c] = registers[a] & b; } } },
{"borr", { [®isters](const int a, const int b, const int c) { registers[c] = registers[a] | registers[b]; } } },
{"bori", { [®isters](const int a, const int b, const int c) { registers[c] = registers[a] | b; } } },
{"setr", { [®isters](const int a, const int b, const int c) { registers[c] = registers[a]; } } },
{"seti", { [®isters](const int a, const int b, const int c) { registers[c] = a; } } },
{"gtir", { [®isters](const int a, const int b, const int c) { registers[c] = a > registers[b] ? 1 : 0; } } },
{"gtri", { [®isters](const int a, const int b, const int c) { registers[c] = registers[a] > b ? 1 : 0; } } },
{"gtrr", { [®isters](const int a, const int b, const int c) { registers[c] = registers[a] > registers[b] ? 1 : 0; } } },
{"eqir", { [®isters](const int a, const int b, const int c) { registers[c] = a == registers[b] ? 1 : 0; } } },
{"eqri", { [®isters](const int a, const int b, const int c) { registers[c] = registers[a] == b ? 1 : 0; } } },
{"eqrr", { [®isters](const int a, const int b, const int c) { registers[c] = registers[a] == registers[b] ? 1 : 0; } } }
};
std::getline(file, line);
const auto instr_reg = line[4] - '0';
std::vector<std::pair<std::string, std::array<int, 3>>> instrs;
while(std::getline(file, line)) {
instrs.emplace_back(parse_input(line));
}
registers = {0, 0, 0, 0, 0, 0};
int instr_p = 0;
while(instr_p < instrs.size()) {
registers[instr_reg] = instr_p;
if (instrs[instr_p].first == "eqrr") {
break;
}
const auto it = map.find(instrs[instr_p].first);
it->second(instrs[instr_p].second[0], instrs[instr_p].second[1], instrs[instr_p].second[2]);
instr_p = registers[instr_reg];
instr_p++;
}
std::cout << registers[instrs[instr_p].second[0]] << '\n';
return 0;
}