aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2023-01-19 13:22:25 +0800
committerkaiwu <kaiwu2004@gmail.com>2023-01-19 13:22:25 +0800
commita5ab1e0bdbb8ccc53199c3783100e971e23a2c7a (patch)
tree74b7af62257307e1db405f9a87551d795bf3453e /src
parent492fc08facfd4c46a7176c67d55ddb4947e88ba6 (diff)
downloadadvent-of-code-a5ab1e0bdbb8ccc53199c3783100e971e23a2c7a.tar.gz
advent-of-code-a5ab1e0bdbb8ccc53199c3783100e971e23a2c7a.zip
2015 done
Diffstat (limited to 'src')
-rw-r--r--src/2015/day25/README.md7
-rw-r--r--src/2015/day25/aoc.cpp49
2 files changed, 55 insertions, 1 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