Advertisement
Guest User

Untitled

a guest
Dec 14th, 2021
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1.  
  2. #include <array>
  3. #include <vector>
  4. #include <future>
  5. #include <limits>
  6. #include <fstream>
  7. #include <iostream>
  8.  
  9. using namespace std;
  10.  
  11. array<char, 26 * 26> lookup{};
  12. int steps = 40;
  13.  
  14. using Frequencies = array<uint64_t, 26>;
  15.  
  16. void expand(char a, char b, int depth, Frequencies &freq)
  17. {
  18.     if (depth == steps) { ++freq[a]; return; }
  19.  
  20.     char toAdd = lookup[a*26 + b];
  21.     expand(a, toAdd, depth + 1, freq);
  22.     expand(toAdd, b, depth + 1, freq);
  23. }
  24.  
  25. int main(int argc, const char** argv)
  26. {
  27.     if (argc > 1) steps = atoi(argv[1]);
  28.     ifstream input("input14.txt");
  29.  
  30.     string prototype;
  31.     input >> prototype;
  32.     for (auto &c : prototype) c -= 'A';
  33.  
  34.     while (true)
  35.     {
  36.         string t1, t2, t3;
  37.         input >> t1 >> t2 >> t3;
  38.         if (t2 != "->") break;
  39.  
  40.         lookup[(t1[0] - 'A') * 26 + (t1[1] - 'A')] = t3[0] - 'A';
  41.     }
  42.  
  43.     Frequencies freq{};
  44.  
  45.     vector<future<Frequencies>> jobs;
  46.     for (size_t i = 0; i < prototype.size() - 1; ++i)
  47.         jobs.push_back(async(std::launch::async, [a = prototype[i], b = prototype[i + 1]]
  48.             {
  49.                 Frequencies f{};
  50.                 expand(a, b, 0, f);
  51.                 return f;
  52.             }));
  53.  
  54.     for (auto &j : jobs)
  55.     {
  56.         auto f = j.get();
  57.         for (size_t i = 0; i < f.size(); ++i) freq[i] += f[i];
  58.     }
  59.  
  60.     // Don't forget the final character
  61.     ++freq[prototype[prototype.size() - 1]];
  62.  
  63.     uint64_t biggest = 0;
  64.     uint64_t smallest = numeric_limits<uint64_t>::max();
  65.     for (auto &count : freq)
  66.     {
  67.         if (count == 0) continue;
  68.         biggest = max(biggest, count);
  69.         smallest = min(smallest, count);
  70.     }
  71.  
  72.     cout << (biggest - smallest) << endl;
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement