aboutsummaryrefslogtreecommitdiff
path: root/2016/src/day19.cpp
diff options
context:
space:
mode:
Diffstat (limited to '2016/src/day19.cpp')
-rw-r--r--2016/src/day19.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/2016/src/day19.cpp b/2016/src/day19.cpp
new file mode 100644
index 0000000..2d7282f
--- /dev/null
+++ b/2016/src/day19.cpp
@@ -0,0 +1,45 @@
1#include <cmath>
2#include <iostream>
3
4using namespace std;
5
6namespace {
7
8template<typename T>
9T
10fn(T N)
11 requires std::is_integral_v<T>
12{
13 return T(1) << static_cast<T>(floor(log2(N)));
14}
15
16void
17part1(unsigned N)
18{
19 cout << (2 * (N - fn(N))) + 1 << '\n';
20}
21
22void
23part2(unsigned N)
24{
25 auto log_3 = [](double x) { return log(x) / log(3); };
26
27 auto threes = pow(3, floor(log_3(N - 1)));
28 if ( N <= 2 * threes ) {
29 cout << static_cast<unsigned>(N - threes);
30 }
31 else {
32 cout << static_cast<unsigned>(N - threes + N - (2 * threes));
33 }
34 cout << '\n';
35}
36
37} // namespace
38
39int
40main()
41{
42 const unsigned puzzle = 3017957;
43 part1(puzzle);
44 part2(puzzle);
45}