#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 day25(line_view lv) { return {code_by(2947, 3029), 0}; } } // namespace aoc2015