Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <filesystem>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <map>
  7. #include <set>
  8. #include <chrono>
  9. #include <thread>
  10. #include <future>
  11.  
  12. #include "../../../_Headers/IO.h"
  13. #include "../../../_Headers/Maths.h"
  14.  
  15. namespace fs = std::experimental::filesystem;
  16. using std::cout;
  17. using std::cin;
  18. using std::endl;
  19.  
  20. unsigned n_threads = std::thread::hardware_concurrency();
  21.  
  22. int F_Read_File_To_Array(std::experimental::filesystem::path path, std::vector<std::string>& str)
  23. {
  24.     if (!std::experimental::filesystem::exists(path))
  25.     {
  26.         std::cout << "ERROR in F_Read_File_To_Array: Path does not exist!" << std::endl;
  27.         return 0;
  28.     }
  29.  
  30.     std::ifstream infile(path);
  31.     if (infile.is_open())
  32.     {
  33.         std::string line = "";
  34.         while (std::getline(infile, line))
  35.         {
  36.             str.push_back(line);
  37.         }
  38.         return 1;
  39.     }
  40.     return 0;
  41. }
  42.  
  43. int F_Convert_Strings_To_Ints(std::vector<std::string> str, std::vector<std::vector<int>>& inp)
  44. {
  45.     for (int i = 0; i < str.size(); i++)
  46.     {
  47.         std::stringstream ss(str[i]);
  48.         std::vector<int> pb;
  49.         std::string t = "";
  50.         while (ss >> t)
  51.         {
  52.             std::istringstream iss(t);
  53.             int i = 0;
  54.             if (iss >> i)
  55.             {
  56.                 pb.push_back(i);
  57.             }
  58.         }
  59.         inp.push_back(pb);
  60.     }
  61.     return 1;
  62. }
  63.  
  64. int Fun(int start, int end, std::vector<std::vector<int>> vint)
  65. {
  66.     int current_severity = 0;
  67.     for (int delay = start; end > 0 ? delay <= end : true; delay++) //while (end > 0 ? delay < end : true)
  68.     {
  69.         int last_ind = 0;
  70.         current_severity = 0;
  71.         for (int pos = 0; pos <= vint.back()[0]; pos++)
  72.         {
  73.             int ind = -1;
  74.             for (int i = last_ind; i < vint.size(); i++)
  75.             {
  76.                 if (vint[i][0] == pos)
  77.                 {
  78.                     ind = i;
  79.                     last_ind = ind;
  80.                 }
  81.                 else if (vint[i][0] > pos)
  82.                 {
  83.                     break;
  84.                 }
  85.             }
  86.             if (ind >= 0)
  87.             {
  88.                 if (((vint[ind][0] + delay) % (2 * (vint[ind][1] - 1))) == 0)
  89.                 {
  90.                     current_severity += vint[ind][0] * vint[ind][1];
  91.                     if (delay != 0)
  92.                     {
  93.                         current_severity += 1;
  94.                         break;
  95.                     }
  96.                 }
  97.             }
  98.         }
  99.         if (current_severity == 0)
  100.         {
  101.             return delay;
  102.         }
  103.     }
  104.     return -current_severity;
  105. }
  106.  
  107.  
  108. int main(void)
  109. {
  110.     std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
  111.  
  112.     //fs::path path("../../_Input/input_Day13.txt");
  113.     //fs::path path("../../_Input/input_Day13_test.txt");
  114.     //fs::path path("../../_Input/input_Day13_8d.txt");
  115.     fs::path path("../../_Input/input_Day13_9d.txt");
  116.     std::vector<std::string> str;
  117.     std::vector<std::vector<int>> vint;
  118.     F_Read_File_To_Array(path, str);
  119.     F_Convert_Strings_To_Ints(str, vint);
  120.  
  121.  
  122.     cout << "Threads: " << n_threads << endl;
  123.  
  124.     int delay = 0;
  125.  
  126.     while (true)
  127.     {
  128.         int delay_range = 80000;
  129.  
  130.         std::vector<std::future<int>> funcs;
  131.         for (unsigned i = 0; i < n_threads; i++)
  132.         {
  133.             funcs.push_back(std::async(std::launch::async, Fun, delay, delay + delay_range, vint));
  134.             delay += delay_range;
  135.         }
  136.  
  137.         std::vector<int> results;
  138.         for (auto&& f : funcs)
  139.         {
  140.             results.push_back(f.get());
  141.         }
  142.  
  143.         if (std::any_of(results.begin(), results.end(), [](int n) {return n > 0;}))
  144.         {
  145.             for (auto&& r : results)
  146.             {
  147.                 if (r > 0)
  148.                 {
  149.                     cout << "Delay to not get caught: " << r << endl;
  150.                 }
  151.             }
  152.             break;
  153.         }
  154.     }
  155.  
  156.     F_Print_Time("Total duration: ", t1);
  157.  
  158.     system("pause");
  159.     return 1;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement