Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <numeric>
- #include <chrono>
- typedef unsigned long ulong;
- template<int age>
- struct initial {
- static constexpr ulong RET() {
- ulong input[300] {
- #include "input.txt"
- };
- ulong result = 0;
- for(const ulong n : input) {
- if(n == age) {
- ++result;
- }
- }
- return result;
- }
- };
- template<>
- struct initial<0> {
- static constexpr ulong RET() {
- return 0;
- }
- };
- template<bool condition, typename THEN, typename ELSE>
- struct IF {
- typedef THEN RET;
- };
- template<typename THEN, typename ELSE>
- struct IF<false, THEN, ELSE> {
- typedef ELSE RET;
- };
- template<int generation, int age>
- struct value {
- typedef typename IF<(generation > 0), value<generation - 1, age + 1>, initial<age>>::RET computor;
- static constexpr ulong RET() {
- return computor::RET();
- }
- };
- template<int generation>
- struct value<generation, 8> {
- typedef typename IF<(generation > 0), value<generation - 1, 0>, initial<8>>::RET computor;
- static constexpr ulong RET() {
- return computor::RET();
- }
- };
- template<typename T1, typename T2>
- struct sum {
- static constexpr ulong RET() {
- return T1::RET() + T2::RET();
- }
- };
- template<int generation>
- struct value<generation, 6> {
- typedef typename IF<(generation > 0), sum<value<generation - 1, 0>, value<generation - 1, 7>>, initial<6>>::RET computor;
- static constexpr ulong RET() {
- return computor::RET();
- }
- };
- template<int generation>
- struct result {
- static constexpr ulong RET() {
- return value<generation, 0>::RET()
- + value<generation, 1>::RET()
- + value<generation, 2>::RET()
- + value<generation, 3>::RET()
- + value<generation, 4>::RET()
- + value<generation, 5>::RET()
- + value<generation, 6>::RET()
- + value<generation, 7>::RET()
- + value<generation, 8>::RET();
- }
- };
- int main() {
- auto start = std::chrono::high_resolution_clock::now();
- std::cout << "result 1: " << result<80>::RET() << std::endl;
- std::cout << "result 2: " << result<256>::RET() << std::endl;
- auto stop = std::chrono::high_resolution_clock::now();
- auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);
- std::cout << "chrono time: " << duration.count() << "ns" << std::endl;
- auto duration2 = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
- std::cout << "chrono time: " << duration2.count() << "ms" << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement