From 3a236e5e3c301abe2a1abe6ae072b3aa81a3cfe4 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Thu, 2 Jan 2025 17:12:07 +0100 Subject: aoc 2024, day 24, cleanup --- 2024/src/day24.cpp | 89 ++++++++++++++++++++++++------------------------------ 1 file changed, 39 insertions(+), 50 deletions(-) (limited to '2024') diff --git a/2024/src/day24.cpp b/2024/src/day24.cpp index 3f252a1..c2bd505 100644 --- a/2024/src/day24.cpp +++ b/2024/src/day24.cpp @@ -114,15 +114,6 @@ part1(const tuple, map tmp{}; - snprintf(tmp.data(), tmp.size(), "%c%02lu", chr, num); // NOLINT - return tmp.data(); -} - class RippleCarryAdder { public: explicit RippleCarryAdder(const tuple, map>>& data) @@ -131,12 +122,6 @@ public: { } - RippleCarryAdder(const map>& deps, unsigned long x_value, unsigned long y_value) - : deps_(deps) - { - build_inputs(x_value, y_value); - } - void swap_wire(const string& lhs, const string& rhs) { swap(deps_.at(lhs), deps_.at(rhs)); @@ -197,6 +182,14 @@ public: } private: + static string make_wire(char chr, unsigned long num) + { + static const size_t MAX_LEN = 10; + array tmp{}; + snprintf(tmp.data(), tmp.size(), "%c%02lu", chr, num); // NOLINT + return tmp.data(); + } + void build_inputs(unsigned long x_value, unsigned long y_value) { inputs_.clear(); @@ -269,54 +262,50 @@ part2(const tuple, map solution; for ( auto bit = 0UL; bit != MAX_BITS; ++bit ) { - const auto x_value = 1UL << bit; // (1UL << (bit+1)) - 1; - const auto y_value = 0UL << bit; + const auto x_value = 1UL << bit; + const auto y_value = 0UL; if ( !rca.eval_z(x_value, y_value) ) { return; } const auto failed_bits = rca.test(x_value, y_value); - if ( !failed_bits.empty() ) { - set candidates; - for ( const auto& wire: failed_bits ) { - rca.collect(wire, candidates); - } - - const vector foo{ candidates.begin(), candidates.end() }; - - for ( auto i = foo.begin(); i != foo.end(); ++i ) { - for ( auto j = i + 1; j != foo.end(); ++j ) { - auto do_test = [&](unsigned long lhs, unsigned long rhs) -> bool { - RippleCarryAdder rca_tester{ data }; + if ( failed_bits.empty() ) { + continue; + } - rca_tester.swap_wire(*i, *j); + set unique_candidates; + for ( const auto& wire: failed_bits ) { + rca.collect(wire, unique_candidates); + } - if ( !rca_tester.eval_z(lhs, rhs) ) { - return false; - } - return rca_tester.test(lhs, rhs).empty(); - }; + const vector candidates{ unique_candidates.begin(), unique_candidates.end() }; - if ( do_test(x_value, y_value) ) { - const auto x_value2 = 7UL << (bit - 1); - const auto y_value2 = 3UL << (bit - 1); + for ( auto i = candidates.begin(); i != candidates.end(); ++i ) { + for ( auto j = i + 1; j != candidates.end(); ++j ) { + auto do_test = [&](const tuple& test_value) -> bool { + const auto [lhs, rhs] = test_value; - if ( do_test(x_value2, y_value2) ) { - const auto x_value3 = 1UL << bit; - const auto y_value3 = 1UL << bit; + RippleCarryAdder rca_tester{ data }; - if ( do_test(x_value3, y_value3) ) { - const auto x_value4 = 5UL << (bit - 1); - const auto y_value4 = 3UL << (bit - 1); + rca_tester.swap_wire(*i, *j); - if ( do_test(x_value4, y_value4) ) { - solution.insert(*i); - solution.insert(*j); - } - } - } + if ( !rca_tester.eval_z(lhs, rhs) ) { + return false; } + return rca_tester.test(lhs, rhs).empty(); + }; + + const vector> test_values = { + { x_value, y_value }, + { 7UL << (bit - 1), 3UL << (bit - 1) }, + { 1UL << bit, 1UL << bit }, + { 5UL << (bit - 1), 3UL << (bit - 1) } + }; + + if ( ranges::all_of(test_values, do_test) ) { + solution.insert(*i); + solution.insert(*j); } } } -- cgit v1.3