Branch data Line data Source code
1 : : #include <iostream>
2 : : #include <fstream>
3 : : #include <string>
4 : : #include <unordered_map>
5 : : #include <queue>
6 : : #include <tuple>
7 : : #include <cstdint>
8 : : using namespace std;
9 : :
10 : 2 : int main() {
11 : 2 : ifstream inputFile("input.txt");
12 [ + + ]: 2 : if (!inputFile) {
13 : 1 : cerr << "Error: Could not open input file." << endl;
14 : 1 : return 1;
15 : : }
16 : :
17 : 1 : unordered_map<string, bool> wireValues;
18 : 1 : queue<tuple<string, string, string, string>> pendingGates;
19 : :
20 [ + - + + : 91 : for (string line; getline(inputFile, line) && !line.empty(); wireValues[line.substr(0, 3)] = line[5] - '0');
+ + ]
21 : :
22 [ + + ]: 223 : for (string in1, in2, op, out, _; inputFile >> in1 >> op >> in2 >> _ >> out; pendingGates.emplace(in1, in2, op, out));
23 : :
24 [ + + ]: 3001 : while (!pendingGates.empty()) {
25 : 3000 : auto [input1, input2, op, output] = pendingGates.front();
26 : 3000 : pendingGates.pop();
27 : :
28 [ + + + + : 3000 : if (wireValues.contains(input1) && wireValues.contains(input2)) {
+ + ]
29 : 222 : bool v1 = wireValues[input1];
30 : 222 : bool v2 = wireValues[input2];
31 [ + + ]: 222 : if (op == "AND") {
32 : 89 : wireValues[output] = v1 & v2;
33 [ + + ]: 133 : } else if (op == "OR") {
34 : 44 : wireValues[output] = v1 | v2;
35 : : } else {
36 : 89 : wireValues[output] = v1 ^ v2;
37 : : }
38 : : } else {
39 : 2778 : pendingGates.emplace(input1, input2, op, output);
40 : : }
41 : 3000 : }
42 : :
43 : 1 : uint64_t binaryResult = 0;
44 [ + + ]: 313 : for (const auto& [wire, value] : wireValues) {
45 [ + + ]: 312 : if (wire[0] == 'z') {
46 : 138 : binaryResult |= (uint64_t(value) << stoi(&wire[1]));
47 : : }
48 : : }
49 : :
50 : 1 : cout << binaryResult << endl;
51 [ + + ]: 3 : }
|