1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#include "aoc.h"
namespace aoc2015 {
/* 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
|