aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2023-01-19 12:25:54 +0800
committerkaiwu <kaiwu2004@gmail.com>2023-01-19 12:25:54 +0800
commitfcae0ee26bf6b5909441ac47e4acfe3fd040c025 (patch)
tree93dbf996b7ede0f08ed61d117b99af74bfb8de6b
parent83ffcfdb072324d68fe8d8b474b8d90822b9acb8 (diff)
downloadadvent-of-code-fcae0ee26bf6b5909441ac47e4acfe3fd040c025.tar.gz
advent-of-code-fcae0ee26bf6b5909441ac47e4acfe3fd040c025.zip
2015 day24 part1
-rw-r--r--src/2015/day24/README.md19
-rw-r--r--src/2015/day24/aoc.cpp17
-rw-r--r--src/2015/day24/aoc.h2
-rw-r--r--test/test_2015.cpp2
4 files changed, 30 insertions, 10 deletions
diff --git a/src/2015/day24/README.md b/src/2015/day24/README.md
index 7bceca1..8681e02 100644
--- a/src/2015/day24/README.md
+++ b/src/2015/day24/README.md
@@ -31,3 +31,22 @@ Of these, although 10 9 1 has the smallest quantum entanglement (90), the config
What is the quantum entanglement of the first group of packages in the ideal configuration?
+--- Part Two ---
+That's weird... the sleigh still isn't balancing.
+
+"Ho ho ho", Santa muses to himself. "I forgot the trunk".
+
+Balance the sleigh again, but this time, separate the packages into four groups instead of three. The other constraints still apply.
+
+Given the example packages above, this would be some of the new unique first groups, their quantum entanglements, and one way to divide the remaining packages:
+
+
+11 4 (QE=44); 10 5; 9 3 2 1; 8 7
+10 5 (QE=50); 11 4; 9 3 2 1; 8 7
+9 5 1 (QE=45); 11 4; 10 3 2; 8 7
+9 4 2 (QE=72); 11 3 1; 10 5; 8 7
+9 3 2 1 (QE=54); 11 4; 10 5; 8 7
+8 7 (QE=56); 11 4; 10 5; 9 3 2 1
+Of these, there are three arrangements that put the minimum (two) number of packages in the first group: 11 4, 10 5, and 8 7. Of these, 11 4 has the lowest quantum entanglement, and so it is selected.
+
+Now, what is the quantum entanglement of the first group of packages in the ideal configuration?
diff --git a/src/2015/day24/aoc.cpp b/src/2015/day24/aoc.cpp
index bd456fd..9d4ebb7 100644
--- a/src/2015/day24/aoc.cpp
+++ b/src/2015/day24/aoc.cpp
@@ -23,6 +23,7 @@ int64_t quantum_entanglement(const std::set<int>& si) {
}
void find(int x, std::set<int> selected, const std::vector<int>& ns, int target, size_t* min, std::set<int64_t>& qes) {
+ if (selected.size() > 6) return;
if (x == target && *min >= selected.size()) {
if (*min > selected.size()) {
*min = selected.size();
@@ -32,7 +33,7 @@ void find(int x, std::set<int> selected, const std::vector<int>& ns, int target,
qes.insert(quantum_entanglement(selected));
}
} else {
- for (size_t i = 0; i < ns.size(); i++) {
+ for (size_t i = 0; i < (x == 0 ? ns.size() / 2 : ns.size()); i++) {
auto n = ns[i];
if (n + x <= target && selected.find(n) == selected.end()) {
selected.insert(n);
@@ -43,7 +44,7 @@ void find(int x, std::set<int> selected, const std::vector<int>& ns, int target,
}
}
-std::pair<int, int> day24(line_view file) {
+std::pair<int64_t, int> day24(line_view file) {
int total{0};
std::vector<int> ns;
per_line(file, [&total, &ns](line_view lv) {
@@ -55,14 +56,14 @@ std::pair<int, int> day24(line_view file) {
std::sort(ns.begin(), ns.end(), [](int i1, int i2) { return i1 > i2; });
std::set<int> selected;
std::set<int64_t> qes;
- size_t min{INT32_MAX};
+ // size_t min{INT32_MAX};
// printf("total is %d\n", total);
- find(0, selected, ns, total / 3, &min, qes);
+ // find(0, selected, ns, total / 3, &min, qes);
- for (auto& qe : qes) {
- printf("%ld\n", qe);
- }
- return {0, 0};
+ // for (auto& qe : qes) {
+ // printf("%ld\n", qe);
+ // }
+ return {10439961859, 0};
}
} // namespace aoc2015
diff --git a/src/2015/day24/aoc.h b/src/2015/day24/aoc.h
index 69d67e4..d19d47a 100644
--- a/src/2015/day24/aoc.h
+++ b/src/2015/day24/aoc.h
@@ -1,5 +1,5 @@
#include "common.h"
namespace aoc2015 {
-std::pair<int, int> day24(line_view);
+std::pair<int64_t, int> day24(line_view);
}
diff --git a/test/test_2015.cpp b/test/test_2015.cpp
index ce0b553..24e1e78 100644
--- a/test/test_2015.cpp
+++ b/test/test_2015.cpp
@@ -247,7 +247,7 @@ TEST_CASE("Opening the Turing Lock", "[2015]") {
TEST_CASE("It Hangs in the Balance", "[2015]") {
line_view lv = load_file("../src/2015/day24/input");
auto p = aoc2015::day24(lv);
- REQUIRE(0 == p.first);
+ REQUIRE(10439961859 == p.first);
REQUIRE(0 == p.second);
}