Skip to content

Commit db016e6

Browse files
committed
day 24 part 1
1 parent ab54306 commit db016e6

File tree

1 file changed

+10
-29
lines changed

1 file changed

+10
-29
lines changed

2024/src/day24.cpp

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ struct Operation {
3535
};
3636

3737
struct Data {
38-
std::map<std::string, int> gates;
38+
std::map<std::string, int64_t> gates;
3939
std::vector<Operation> operations;
4040
};
4141

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) {
4343
return gates.at(op.arg1) != -1 && gates.at(op.arg2) != -1;
4444
}
4545

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) {
4747
std::sort(operations.begin() + offset, operations.end(), [&](const Operation& a, const Operation& b) {
4848
int valid_args_a = 0;
4949
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
5555
});
5656
}
5757

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;
6060
int64_t result = 0;
6161
for(auto& [gate, value] : gates) {
6262
if (gate[0] == 'z') {
63-
std::cout << std::format(" {} {}\n", gate, value);
6463
result |= value << idx;
6564
++idx;
6665
}
@@ -73,27 +72,13 @@ int64_t part1(const Data &data)
7372
auto operations = data.operations;
7473
auto gates = data.gates;
7574

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-
8575
put_valid_gates_first(operations, gates);
8676

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-
9277
for(size_t i = 0; i < operations.size(); ++i) {
9378
auto& op = operations[i];
9479
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);
9782
int result = 0;
9883
switch(op.gate) {
9984
case Gate::AND:
@@ -112,13 +97,9 @@ int64_t part1(const Data &data)
11297
}
11398
else {
11499
put_valid_gates_first(operations, gates, i);
115-
// --i;
100+
--i;
116101
}
117102
}
118-
for(auto& [gate, value] : gates) {
119-
std::cout << gate << " " << value << std::endl;
120-
}
121-
std::cout << std::endl;
122103
return form_number(gates);
123104
}
124105

@@ -224,7 +205,7 @@ int main(int argc, char **argv)
224205
{
225206
Data data = parse();
226207

227-
int64_t answer1 = 0;
208+
int64_t answer1 = 45923082839246;
228209
int64_t answer2 = 0;
229210

230211
auto first = part1(data);
@@ -243,4 +224,4 @@ int main(int argc, char **argv)
243224
return 0;
244225
}
245226
}
246-
}
227+
}

0 commit comments

Comments
 (0)