Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <filesystem>
- #include <fstream>
- #include <sstream>
- #include <map>
- #include <set>
- #include <chrono>
- #include <thread>
- #include <future>
- #include "../../../_Headers/IO.h"
- #include "../../../_Headers/Maths.h"
- namespace fs = std::experimental::filesystem;
- using std::cout;
- using std::cin;
- using std::endl;
- unsigned n_threads = std::thread::hardware_concurrency();
- int F_Read_File_To_Array(std::experimental::filesystem::path path, std::vector<std::string>& str)
- {
- if (!std::experimental::filesystem::exists(path))
- {
- std::cout << "ERROR in F_Read_File_To_Array: Path does not exist!" << std::endl;
- return 0;
- }
- std::ifstream infile(path);
- if (infile.is_open())
- {
- std::string line = "";
- while (std::getline(infile, line))
- {
- str.push_back(line);
- }
- return 1;
- }
- return 0;
- }
- int F_Convert_Strings_To_Ints(std::vector<std::string> str, std::vector<std::vector<int>>& inp)
- {
- for (int i = 0; i < str.size(); i++)
- {
- std::stringstream ss(str[i]);
- std::vector<int> pb;
- std::string t = "";
- while (ss >> t)
- {
- std::istringstream iss(t);
- int i = 0;
- if (iss >> i)
- {
- pb.push_back(i);
- }
- }
- inp.push_back(pb);
- }
- return 1;
- }
- int Fun(int start, int end, std::vector<std::vector<int>> vint)
- {
- int current_severity = 0;
- for (int delay = start; end > 0 ? delay <= end : true; delay++) //while (end > 0 ? delay < end : true)
- {
- int last_ind = 0;
- current_severity = 0;
- for (int pos = 0; pos <= vint.back()[0]; pos++)
- {
- int ind = -1;
- for (int i = last_ind; i < vint.size(); i++)
- {
- if (vint[i][0] == pos)
- {
- ind = i;
- last_ind = ind;
- }
- else if (vint[i][0] > pos)
- {
- break;
- }
- }
- if (ind >= 0)
- {
- if (((vint[ind][0] + delay) % (2 * (vint[ind][1] - 1))) == 0)
- {
- current_severity += vint[ind][0] * vint[ind][1];
- if (delay != 0)
- {
- current_severity += 1;
- break;
- }
- }
- }
- }
- if (current_severity == 0)
- {
- return delay;
- }
- }
- return -current_severity;
- }
- int main(void)
- {
- std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
- //fs::path path("../../_Input/input_Day13.txt");
- //fs::path path("../../_Input/input_Day13_test.txt");
- //fs::path path("../../_Input/input_Day13_8d.txt");
- fs::path path("../../_Input/input_Day13_9d.txt");
- std::vector<std::string> str;
- std::vector<std::vector<int>> vint;
- F_Read_File_To_Array(path, str);
- F_Convert_Strings_To_Ints(str, vint);
- cout << "Threads: " << n_threads << endl;
- int delay = 0;
- while (true)
- {
- int delay_range = 80000;
- std::vector<std::future<int>> funcs;
- for (unsigned i = 0; i < n_threads; i++)
- {
- funcs.push_back(std::async(std::launch::async, Fun, delay, delay + delay_range, vint));
- delay += delay_range;
- }
- std::vector<int> results;
- for (auto&& f : funcs)
- {
- results.push_back(f.get());
- }
- if (std::any_of(results.begin(), results.end(), [](int n) {return n > 0;}))
- {
- for (auto&& r : results)
- {
- if (r > 0)
- {
- cout << "Delay to not get caught: " << r << endl;
- }
- }
- break;
- }
- }
- F_Print_Time("Total duration: ", t1);
- system("pause");
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement