Advertisement
Guest User

Untitled

a guest
Dec 6th, 2021
2,919
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <numeric>
  5.  
  6. #include <chrono>
  7.  
  8. typedef unsigned long ulong;
  9.  
  10. template<int age>
  11. struct initial {
  12.     static constexpr ulong RET() {
  13.         ulong input[300] {
  14. #include "input.txt"
  15.         };
  16.  
  17.         ulong result = 0;
  18.         for(const ulong n : input) {
  19.             if(n == age) {
  20.                 ++result;
  21.             }
  22.         }
  23.         return result;
  24.     }
  25. };
  26.  
  27. template<>
  28. struct initial<0> {
  29.     static constexpr ulong RET() {
  30.         return 0;
  31.     }
  32. };
  33.  
  34. template<bool condition, typename THEN, typename ELSE>
  35. struct IF {
  36.     typedef THEN RET;
  37. };
  38.  
  39. template<typename THEN, typename ELSE>
  40. struct IF<false, THEN, ELSE> {
  41.     typedef ELSE RET;
  42. };
  43.  
  44. template<int generation, int age>
  45. struct value {
  46.     typedef typename IF<(generation > 0), value<generation - 1, age + 1>, initial<age>>::RET computor;
  47.     static constexpr ulong RET() {
  48.         return computor::RET();
  49.     }
  50. };
  51.  
  52. template<int generation>
  53. struct value<generation, 8> {
  54.     typedef typename IF<(generation > 0), value<generation - 1, 0>, initial<8>>::RET computor;
  55.     static constexpr ulong RET() {
  56.         return computor::RET();
  57.     }
  58. };
  59.  
  60. template<typename T1, typename T2>
  61. struct sum {
  62.     static constexpr ulong RET() {
  63.         return T1::RET() + T2::RET();
  64.     }
  65. };
  66.  
  67. template<int generation>
  68. struct value<generation, 6> {
  69.     typedef typename IF<(generation > 0), sum<value<generation - 1, 0>, value<generation - 1, 7>>, initial<6>>::RET computor;
  70.     static constexpr ulong RET() {
  71.         return computor::RET();
  72.     }
  73. };
  74.  
  75. template<int generation>
  76. struct result {
  77.     static constexpr ulong RET() {
  78.         return value<generation, 0>::RET()
  79.         + value<generation, 1>::RET()
  80.         + value<generation, 2>::RET()
  81.         + value<generation, 3>::RET()
  82.         + value<generation, 4>::RET()
  83.         + value<generation, 5>::RET()
  84.         + value<generation, 6>::RET()
  85.         + value<generation, 7>::RET()
  86.         + value<generation, 8>::RET();
  87.     }
  88. };
  89.  
  90. int main() {
  91.     auto start = std::chrono::high_resolution_clock::now();
  92.  
  93.     std::cout << "result 1: " << result<80>::RET() << std::endl;
  94.     std::cout << "result 2: " << result<256>::RET() << std::endl;
  95.  
  96.     auto stop = std::chrono::high_resolution_clock::now();
  97.  
  98.     auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);
  99.     std::cout << "chrono time: " << duration.count()  << "ns" << std::endl;
  100.     auto duration2 = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
  101.     std::cout << "chrono time: " << duration2.count()  << "ms" << std::endl;
  102.  
  103.     return 0;
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement