diff options
Diffstat (limited to '2015/src/day25.cpp')
| -rw-r--r-- | 2015/src/day25.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/2015/src/day25.cpp b/2015/src/day25.cpp new file mode 100644 index 0000000..ed02c9b --- /dev/null +++ b/2015/src/day25.cpp | |||
| @@ -0,0 +1,53 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <sstream> | ||
| 4 | #include <vector> | ||
| 5 | using namespace std; | ||
| 6 | |||
| 7 | tuple<long, long> | ||
| 8 | read_file(string_view filename) | ||
| 9 | { | ||
| 10 | fstream input{ filename }; | ||
| 11 | |||
| 12 | vector<long> values; | ||
| 13 | for ( string word; input >> word; ) { | ||
| 14 | long value{}; | ||
| 15 | if ( stringstream(word) >> value ) { | ||
| 16 | values.push_back(value); | ||
| 17 | } | ||
| 18 | } | ||
| 19 | return { values.at(0), values.at(1) }; | ||
| 20 | } | ||
| 21 | |||
| 22 | long | ||
| 23 | triangle_num(long row, long col) | ||
| 24 | { | ||
| 25 | const auto side = row + col - 1; | ||
| 26 | return (side * (side + 1)) / 2 - row; | ||
| 27 | } | ||
| 28 | |||
| 29 | long | ||
| 30 | powmod(long base, long pow, long mod) | ||
| 31 | { | ||
| 32 | long result = 0; | ||
| 33 | for ( result = 1; pow != 0; pow /= 2 ) { | ||
| 34 | if ( pow % 2 == 1 ) { | ||
| 35 | result = (result * base) % mod; | ||
| 36 | } | ||
| 37 | base = (base * base) % mod; | ||
| 38 | } | ||
| 39 | return result; | ||
| 40 | } | ||
| 41 | |||
| 42 | void | ||
| 43 | part1(long row, long col) | ||
| 44 | { | ||
| 45 | cout << (powmod(252533, triangle_num(row, col), 33554393) * 20151125) % 33554393 << endl; | ||
| 46 | } | ||
| 47 | |||
| 48 | int | ||
| 49 | main() | ||
| 50 | { | ||
| 51 | const auto [row, col] = read_file("data/day25.txt"); | ||
| 52 | part1(row, col); | ||
| 53 | } | ||
