-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_16a.cpp
93 lines (85 loc) · 3.62 KB
/
day_16a.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
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <unordered_map>
#include <functional>
#include <sstream>
std::array<int, 4> parse_input(const std::string& s_main) {
std::string s = s_main;
std::array<int, 4> result;
s.erase(std::remove(std::begin(s), std::end(s), ','), std::end(s));
std::string delimiter = " ";
std::size_t start = 0;
auto end = s.find(delimiter);
int count = 0;
while (end != std::string::npos) {
result[count] = std::stoi(s.substr(start, end - start));
start = end + delimiter.size();
end = s.find(delimiter, start);
count++;
}
result[count] = std::stoi(s.substr(start, s.size() - start));
return result;
}
int main(int argc, char * argv []) {
std::string input = "../input/day_16_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string line;
std::array<int, 4> registers;
std::array<std::function<void(const int, const int, const int)>, 16> functions {{
{ [®isters](const int a, const int b, const int c) { registers[c] = registers[a] + registers[b]; } },
{ [®isters](const int a, const int b, const int c) { registers[c] = registers[a] + b; } },
{ [®isters](const int a, const int b, const int c) { registers[c] = registers[a] * registers[b]; } },
{ [®isters](const int a, const int b, const int c) { registers[c] = registers[a] * b; } },
{ [®isters](const int a, const int b, const int c) { registers[c] = registers[a] & registers[b]; } },
{ [®isters](const int a, const int b, const int c) { registers[c] = registers[a] & b; } },
{ [®isters](const int a, const int b, const int c) { registers[c] = registers[a] | registers[b]; } },
{ [®isters](const int a, const int b, const int c) { registers[c] = registers[a] | b; } },
{ [®isters](const int a, const int b, const int c) { registers[c] = registers[a]; } },
{ [®isters](const int a, const int b, const int c) { registers[c] = a; } },
{ [®isters](const int a, const int b, const int c) { registers[c] = a > registers[b] ? 1 : 0; } },
{ [®isters](const int a, const int b, const int c) { registers[c] = registers[a] > b ? 1 : 0; } },
{ [®isters](const int a, const int b, const int c) { registers[c] = registers[a] > registers[b] ? 1 : 0; } },
{ [®isters](const int a, const int b, const int c) { registers[c] = a == registers[b] ? 1 : 0; } },
{ [®isters](const int a, const int b, const int c) { registers[c] = registers[a] == b ? 1 : 0; } },
{ [®isters](const int a, const int b, const int c) { registers[c] = registers[a] == registers[b] ? 1 : 0; } }
}};
std::array<int, 4> expected_result;
std::array<int, 4> init_state;
std::array<int, 4> instr;
int ans = 0;
bool before_seen = false;
int blank_line_count = 0;
while(blank_line_count < 3 && std::getline(file, line)) {
if (line[0] == 'B') {
blank_line_count = 0;
init_state = parse_input(line.substr(9, line.size() - 1 - 9));
before_seen = true;
} else if (!line.empty() && before_seen) {
instr = parse_input(line);
before_seen = false;
} else if (line[0] == 'A') { // only one that does not call continue
expected_result = parse_input(line.substr(9, line.size() - 1 - 9));
int correct_outputs = 0;
for (const auto& func : functions) {
registers = init_state;
func(instr[1], instr[2], instr[3]);
if (registers == expected_result) {
correct_outputs++;
}
}
if (correct_outputs >= 3) {
ans++;
}
} else {
blank_line_count++;
}
}
std::cout << ans << '\n';
return 0;
}