Branch data Line data Source code
1 : : #include <iostream>
2 : : #include <vector>
3 : : #include <fstream>
4 : : #include <sstream>
5 : : #include <algorithm>
6 : : #include <ranges>
7 : :
8 : : using namespace std;
9 : :
10 : 36 : int comboValue(int operand, int A, int B, int C) {
11 [ + + + + : 36 : return operand < 4 ? operand : operand == 4 ? A : operand == 5 ? B : C;
+ - ]
12 : : }
13 : :
14 : 2 : int main() {
15 : 2 : ifstream input("input.txt");
16 [ + + ]: 2 : if (!input) {
17 : 1 : cerr << "Error: Could not open input file.\n";
18 : 1 : return 1;
19 : : }
20 : 1 : bool first = true;
21 : 1 : vector<string> contents = ranges::istream_view<string>(input) | ranges::to<vector<string>>();
22 : 1 : int64_t A = stoi(contents[2]);
23 : 1 : int64_t B = stoi(contents[5]);
24 : 1 : int64_t C = stoi(contents[8]);
25 : 17 : vector<int> program= contents[10] | views::split(',') | views::transform([](auto&& part) { return part[0] - '0'; }) | ranges::to<vector<int>>();
26 : :
27 [ + + ]: 73 : for (int instructionPointer = 0; instructionPointer < program.size();) {
28 : 72 : int opcode = program[instructionPointer++];
29 : 72 : int operand = program[instructionPointer++];
30 : :
31 [ + + + + : 72 : switch (opcode) {
+ + - + ]
32 : 9 : case 0: // adv
33 : 9 : A /= 1 << comboValue(operand, A, B, C);
34 : 9 : break;
35 : 18 : case 1: // bxl
36 : 18 : B ^= operand;
37 : 18 : break;
38 : 9 : case 2: // bst
39 : 9 : B = comboValue(operand, A, B, C) & 7;
40 : 9 : break;
41 : 9 : case 3: // jnz
42 [ + + ]: 9 : if (A) {
43 : 8 : instructionPointer = operand;
44 : : }
45 : 9 : break;
46 : 9 : case 4: // bxc
47 : 9 : B ^= C;
48 : 9 : break;
49 : 9 : case 5: // out
50 [ + + ]: 9 : cout << (first ? "" : ",") << (comboValue(operand, A, B, C) & 7);
51 : 9 : first = false;
52 : 9 : break;
53 : 0 : case 6: // bdv
54 : 0 : B = A / (1 << comboValue(operand, A, B, C));
55 : 0 : break;
56 : 9 : default: // cdv
57 : 9 : C = A / (1 << comboValue(operand, A, B, C));
58 : 9 : break;
59 : : }
60 : : }
61 [ + + ]: 3 : }
|