aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/2015/day25/README.md7
-rw-r--r--src/2015/day25/aoc.cpp49
-rw-r--r--test/test_2015.cpp2
3 files changed, 56 insertions, 2 deletions
diff --git a/src/2015/day25/README.md b/src/2015/day25/README.md
index a79a355..28f5d1b 100644
--- a/src/2015/day25/README.md
+++ b/src/2015/day25/README.md
@@ -40,3 +40,10 @@ So, to find the second code (which ends up in row 2, column 1), start with the p
"Now remember", the voice continues, "that's not even all of the first few numbers; for example, you're missing the one at 7,1 that would come before 6,2. But, it should be enough to let your-- oh, it's time for lunch! Bye!" The call disconnects.
Santa looks nervous. Your puzzle input contains the message on the machine's console. What code do you give the machine?
+
+--- Part Two ---
+The machine springs to life, then falls silent again. It beeps. "Insufficient fuel", the console reads. "Fifty stars are required before proceeding. One star is available."
+
+..."one star is available"? You check the fuel tank; sure enough, a lone star sits at the bottom, awaiting its friends. Looks like you need to provide 49 yourself.
+
+You have enough stars to .
diff --git a/src/2015/day25/aoc.cpp b/src/2015/day25/aoc.cpp
index dd622e9..9154c57 100644
--- a/src/2015/day25/aoc.cpp
+++ b/src/2015/day25/aoc.cpp
@@ -2,5 +2,52 @@
namespace aoc2015 {
-std::pair<int64_t, int64_t> day25(line_view lv) { return {0, 0}; }
+/* So, to find the second code (which ends up in row 2, column 1),
+ * start with the previous value, 20151125.
+ * Multiply it by 252533 to get 5088824049625. Then, divide that by 33554393,
+ * which leaves a remainder of 31916031. That remainder is the second code.
+ */
+int64_t code_gen(int64_t code) {
+ code *= 252533;
+ code %= 33554393;
+ return code;
+}
+
+// n = 0 ..
+int64_t code_gen(int64_t code, size_t n) {
+ while (n-- > 0) {
+ code = code_gen(code);
+ }
+ return code;
+}
+
+// n = 1 ..
+int64_t total_number_by(size_t n) {
+ int64_t t{0};
+ for (size_t i = 1; i <= n; i++) {
+ t += i;
+ }
+ return t;
+}
+
+size_t row_number(int64_t r, int64_t c) { return r + c - 1; }
+
+int64_t code_by(size_t r, size_t c) {
+ size_t row = row_number(r, c);
+ int64_t total = total_number_by(row - 1);
+
+ for (size_t x = row; x >= 1; x--) {
+ if (x == r) {
+ return code_gen(20151125, total);
+ }
+ total += 1;
+ }
+ return 0;
+}
+
+// Enter the code at row 2947, column 3029.
+std::pair<int64_t, int64_t> day25(line_view lv) {
+ return {code_by(2947, 3029), 0};
+}
+
} // namespace aoc2015
diff --git a/test/test_2015.cpp b/test/test_2015.cpp
index edd56cb..4b07885 100644
--- a/test/test_2015.cpp
+++ b/test/test_2015.cpp
@@ -254,6 +254,6 @@ TEST_CASE("It Hangs in the Balance", "[2015]") {
TEST_CASE("Let It Snow", "[2015]") {
line_view lv = load_file("../src/2015/day25/input");
auto p = aoc2015::day25(lv);
- REQUIRE(0 == p.first);
+ REQUIRE(19980801 == p.first);
REQUIRE(0 == p.second);
}