aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

day17a.cpp (1362B)


      1 #include <algorithm>
      2 #include <cstdint>
      3 #include <iostream>
      4 #include <map>
      5 #include <queue>
      6 #include <ranges>
      7 #include <set>
      8 #include <sstream>
      9 #include <string>
     10 #include <string_view>
     11 #include <vector>
     12 using namespace std;
     13 
     14 class CPU {
     15 public:
     16 	CPU(uint64_t a, uint64_t b, uint64_t c) : A{a}, B{b}, C{c} {}
     17 
     18 	void process(const vector<uint64_t>& v) {
     19 		for (unsigned ip = 0; ip < v.size(); ) {
     20 			switch (v[ip]) {
     21 			case 0:
     22 				A >>= combo(v[ip+1]);
     23 				ip += 2;
     24 				break;
     25 			case 1:
     26 				B ^= v[ip+1];
     27 				ip += 2;
     28 				break;
     29 			case 2:
     30 				B = combo(v[ip+1]) % 8;
     31 				ip += 2;
     32 				break;
     33 			case 3:
     34 				ip = A == 0 ? ip+2 : v[ip+1];
     35 				break;
     36 			case 4:
     37 				B ^= C;
     38 				ip += 2;
     39 				break;
     40 			case 5:
     41 				cout << combo(v[ip+1])%8 << ",";
     42 				ip += 2;
     43 				break;
     44 			case 6:
     45 				B = A >> combo(v[ip+1]);
     46 				ip += 2;
     47 				break;
     48 			case 7:
     49 				C = A >> combo(v[ip+1]);
     50 				ip += 2;
     51 				break;
     52 			default:
     53 				cout << "Error! Operator " << v[ip] << endl;
     54 				exit(1);
     55 			}
     56 		}
     57 		cout << endl;
     58 	}
     59 private:
     60 	uint64_t A, B, C;
     61 
     62 	uint64_t& reg(uint64_t i) {
     63 		return i == 0 ? A : (i == 1 ? B : C);
     64 	}
     65 
     66 	uint64_t combo(uint64_t i) {
     67 		return i <= 3 ? i : reg(i-4);
     68 	}
     69 };
     70 
     71 int main() {
     72 	uint64_t a, b, c;
     73 	cin >> a >> b >> c;
     74 	CPU cpu(a, b, c);
     75 
     76 	vector<uint64_t> instructions;
     77 	while (cin >> a)
     78 		instructions.push_back(a);
     79 
     80 	cpu.process(instructions);
     81 	return 0;
     82 }