diff options
| author | Thomas Schmucker <ts@its1.de> | 2025-10-23 19:28:20 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2025-10-23 19:28:20 +0200 |
| commit | ae1e96f10b399266825e275ca4a8100ceede9cad (patch) | |
| tree | 707fee5d979ed37504909faad921a2e6711b1cc6 /2016/src/day19.cpp | |
| parent | 0450ecd6a67226ddbaeedde64ef45dfdd72cddd1 (diff) | |
| download | advent-of-code-ae1e96f10b399266825e275ca4a8100ceede9cad.tar.gz advent-of-code-ae1e96f10b399266825e275ca4a8100ceede9cad.tar.bz2 advent-of-code-ae1e96f10b399266825e275ca4a8100ceede9cad.zip | |
days 19-25, aoc 2016
Diffstat (limited to '2016/src/day19.cpp')
| -rw-r--r-- | 2016/src/day19.cpp | 45 |
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 | |||
| 4 | using namespace std; | ||
| 5 | |||
| 6 | namespace { | ||
| 7 | |||
| 8 | template<typename T> | ||
| 9 | T | ||
| 10 | fn(T N) | ||
| 11 | requires std::is_integral_v<T> | ||
| 12 | { | ||
| 13 | return T(1) << static_cast<T>(floor(log2(N))); | ||
| 14 | } | ||
| 15 | |||
| 16 | void | ||
| 17 | part1(unsigned N) | ||
| 18 | { | ||
| 19 | cout << (2 * (N - fn(N))) + 1 << '\n'; | ||
| 20 | } | ||
| 21 | |||
| 22 | void | ||
| 23 | part2(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 | |||
| 39 | int | ||
| 40 | main() | ||
| 41 | { | ||
| 42 | const unsigned puzzle = 3017957; | ||
| 43 | part1(puzzle); | ||
| 44 | part2(puzzle); | ||
| 45 | } | ||
