@@ -35,15 +35,15 @@ struct Operation {
35
35
};
36
36
37
37
struct Data {
38
- std::map<std::string, int > gates;
38
+ std::map<std::string, int64_t > gates;
39
39
std::vector<Operation> operations;
40
40
};
41
41
42
- bool has_both_inputs (const Operation& op, const std::map<std::string, int >& gates) {
42
+ bool has_both_inputs (const Operation& op, const std::map<std::string, int64_t >& gates) {
43
43
return gates.at (op.arg1 ) != -1 && gates.at (op.arg2 ) != -1 ;
44
44
}
45
45
46
- void put_valid_gates_first (std::vector<Operation>& operations, const std::map<std::string, int >& gates, size_t offset = 0 ) {
46
+ void put_valid_gates_first (std::vector<Operation>& operations, const std::map<std::string, int64_t >& gates, size_t offset = 0 ) {
47
47
std::sort (operations.begin () + offset, operations.end (), [&](const Operation& a, const Operation& b) {
48
48
int valid_args_a = 0 ;
49
49
valid_args_a += gates.at (a.arg1 ) != -1 ;
@@ -55,12 +55,11 @@ void put_valid_gates_first(std::vector<Operation>& operations, const std::map<st
55
55
});
56
56
}
57
57
58
- int64_t form_number (const std::map<std::string, int >& gates) {
59
- int idx = 0 ;
58
+ int64_t form_number (const std::map<std::string, int64_t >& gates) {
59
+ int64_t idx = 0 ;
60
60
int64_t result = 0 ;
61
61
for (auto & [gate, value] : gates) {
62
62
if (gate[0 ] == ' z' ) {
63
- std::cout << std::format (" {} {}\n " , gate, value);
64
63
result |= value << idx;
65
64
++idx;
66
65
}
@@ -73,27 +72,13 @@ int64_t part1(const Data &data)
73
72
auto operations = data.operations ;
74
73
auto gates = data.gates ;
75
74
76
- for (auto & [gate, value] : gates) {
77
- std::cout << gate << " " << value << std::endl;
78
- }
79
-
80
- for (auto & op : operations) {
81
- std::cout << std::format (" {} {} {} -> {}\n " , op.arg1 , gate_to_string (op.gate ), op.arg2 , op.target );
82
- }
83
- std::cout << std::endl;
84
-
85
75
put_valid_gates_first (operations, gates);
86
76
87
- for (auto & op : operations) {
88
- std::cout << std::format (" {} {} {} -> {}\n " , op.arg1 , gate_to_string (op.gate ), op.arg2 , op.target );
89
- }
90
- std::cout << std::endl;
91
-
92
77
for (size_t i = 0 ; i < operations.size (); ++i) {
93
78
auto & op = operations[i];
94
79
if (has_both_inputs (op, gates)) {
95
- int arg1 = data. gates .at (op.arg1 );
96
- int arg2 = data. gates .at (op.arg2 );
80
+ int arg1 = gates.at (op.arg1 );
81
+ int arg2 = gates.at (op.arg2 );
97
82
int result = 0 ;
98
83
switch (op.gate ) {
99
84
case Gate::AND:
@@ -112,13 +97,9 @@ int64_t part1(const Data &data)
112
97
}
113
98
else {
114
99
put_valid_gates_first (operations, gates, i);
115
- // --i;
100
+ --i;
116
101
}
117
102
}
118
- for (auto & [gate, value] : gates) {
119
- std::cout << gate << " " << value << std::endl;
120
- }
121
- std::cout << std::endl;
122
103
return form_number (gates);
123
104
}
124
105
@@ -224,7 +205,7 @@ int main(int argc, char **argv)
224
205
{
225
206
Data data = parse ();
226
207
227
- int64_t answer1 = 0 ;
208
+ int64_t answer1 = 45923082839246 ;
228
209
int64_t answer2 = 0 ;
229
210
230
211
auto first = part1 (data);
@@ -243,4 +224,4 @@ int main(int argc, char **argv)
243
224
return 0 ;
244
225
}
245
226
}
246
- }
227
+ }
0 commit comments