ErnestGalbrun

Untitled

Dec 16th, 2021 (edited)
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. using namespace std;
  2.  
  3. int main() {
  4.     auto start = absl::Now();
  5.     string line = "BLAH";
  6.     int curr = 0;
  7.     int pos = -1;
  8.     int next_char = 0;
  9.  
  10.     auto from_hex = [](char c) {
  11.         if (c <='9') return c - '0';
  12.         return c - 'A' + 10;
  13.     };
  14.  
  15.     auto take = [&](int n) {
  16.         long long x = 0;
  17.         for(int i = 0; i < n; ++i) {
  18.             x = x << 1;
  19.             if (pos < 0) {
  20.                 pos = 3;
  21.                 curr = from_hex(line.at(next_char));
  22.                 ++next_char;
  23.             }
  24.             x += ((curr & 1 << pos) != 0) ? 1ll : 0ll;
  25.             --pos;
  26.         }
  27.         return x;
  28.     };
  29.  
  30.     function<long long(int*)> process;
  31.     process = [&](int* bits) {
  32.         take(3); // version
  33.         auto id = take(3);
  34.         if (bits != nullptr) {
  35.             *bits -=6;
  36.         }
  37.         if (id == 4) {
  38.             long long x = 0;
  39.             while(true) {
  40.                 bool last = !take(1);
  41.                 x = x << 4;
  42.                 x += take(4);
  43.                 if (bits != nullptr) {
  44.                     *bits -=5;
  45.                 }
  46.                 if (last) break;
  47.             }
  48.             return x;
  49.         }
  50.         else {
  51.             std::vector<long long> ans;
  52.             if (!take(1)) {
  53.                 int l = take(15);
  54.                 if (bits != nullptr) {
  55.                     *bits -=16;
  56.                     *bits -=l;
  57.                 }
  58.                 while(l > 0) {
  59.                     ans.push_back(process(&l));
  60.                 }
  61.             } else {
  62.                 int l = take(11);
  63.                 if (bits != nullptr) {
  64.                     *bits -=12;
  65.                 }
  66.                 for (int i = 0; i < l; ++i) {
  67.                     ans.push_back(process(bits));
  68.                 }
  69.             }
  70.             switch(id) {
  71.                 case 0: return absl::c_accumulate(ans, 0ll);
  72.                 case 1: return absl::c_accumulate(ans, 1ll, multiplies());
  73.                 case 2: return *absl::c_min_element(ans);
  74.                 case 3: return *absl::c_max_element(ans);
  75.                 case 5: return ans[0] > ans[1] ? 1ll : 0ll;
  76.                 case 6: return ans[0] < ans[1] ? 1ll : 0ll;
  77.                 case 7: return ans[0] == ans[1] ? 1ll: 0ll;
  78.                 default: throw invalid_argument("wrong id");
  79.             }
  80.         }
  81.     };
  82.     cout << process(nullptr) << endl;
  83.     cout << absl::Now() - start << endl;
  84.  
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment