Skip to content

Commit

Permalink
day 24 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
K20shores committed Dec 25, 2024
1 parent ab54306 commit db016e6
Showing 1 changed file with 10 additions and 29 deletions.
39 changes: 10 additions & 29 deletions 2024/src/day24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ struct Operation {
};

struct Data {
std::map<std::string, int> gates;
std::map<std::string, int64_t> gates;
std::vector<Operation> operations;
};

bool has_both_inputs(const Operation& op, const std::map<std::string, int>& gates) {
bool has_both_inputs(const Operation& op, const std::map<std::string, int64_t>& gates) {
return gates.at(op.arg1) != -1 && gates.at(op.arg2) != -1;
}

void put_valid_gates_first(std::vector<Operation>& operations, const std::map<std::string, int>& gates, size_t offset = 0) {
void put_valid_gates_first(std::vector<Operation>& operations, const std::map<std::string, int64_t>& gates, size_t offset = 0) {
std::sort(operations.begin() + offset, operations.end(), [&](const Operation& a, const Operation& b) {
int valid_args_a = 0;
valid_args_a += gates.at(a.arg1) != -1;
Expand All @@ -55,12 +55,11 @@ void put_valid_gates_first(std::vector<Operation>& operations, const std::map<st
});
}

int64_t form_number(const std::map<std::string, int>& gates) {
int idx = 0;
int64_t form_number(const std::map<std::string, int64_t>& gates) {
int64_t idx = 0;
int64_t result = 0;
for(auto& [gate, value] : gates) {
if (gate[0] == 'z') {
std::cout << std::format(" {} {}\n", gate, value);
result |= value << idx;
++idx;
}
Expand All @@ -73,27 +72,13 @@ int64_t part1(const Data &data)
auto operations = data.operations;
auto gates = data.gates;

for(auto& [gate, value] : gates) {
std::cout << gate << " " << value << std::endl;
}

for(auto& op : operations) {
std::cout << std::format("{} {} {} -> {}\n", op.arg1, gate_to_string(op.gate), op.arg2, op.target);
}
std::cout << std::endl;

put_valid_gates_first(operations, gates);

for(auto& op : operations) {
std::cout << std::format("{} {} {} -> {}\n", op.arg1, gate_to_string(op.gate), op.arg2, op.target);
}
std::cout << std::endl;

for(size_t i = 0; i < operations.size(); ++i) {
auto& op = operations[i];
if (has_both_inputs(op, gates)) {
int arg1 = data.gates.at(op.arg1);
int arg2 = data.gates.at(op.arg2);
int arg1 = gates.at(op.arg1);
int arg2 = gates.at(op.arg2);
int result = 0;
switch(op.gate) {
case Gate::AND:
Expand All @@ -112,13 +97,9 @@ int64_t part1(const Data &data)
}
else {
put_valid_gates_first(operations, gates, i);
// --i;
--i;
}
}
for(auto& [gate, value] : gates) {
std::cout << gate << " " << value << std::endl;
}
std::cout << std::endl;
return form_number(gates);
}

Expand Down Expand Up @@ -224,7 +205,7 @@ int main(int argc, char **argv)
{
Data data = parse();

int64_t answer1 = 0;
int64_t answer1 = 45923082839246;
int64_t answer2 = 0;

auto first = part1(data);
Expand All @@ -243,4 +224,4 @@ int main(int argc, char **argv)
return 0;
}
}
}
}

0 comments on commit db016e6

Please sign in to comment.