Advertisement
Guest User

2018-9.cpp

a guest
Jan 14th, 2022
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <vector>
  4.  
  5. long highest(const std::vector<long> &s) {
  6.   long i = 0;
  7.   for (auto j : s) {
  8.     if (j > i) {
  9.       i = j;
  10.     }
  11.   }
  12.   return i;
  13. }
  14.  
  15. void print_marbles(const std::list<int> &m)  {
  16.   for (auto i : m) {
  17.     std::cout << i << " ";
  18.   }
  19.   std::cout << '\n';;
  20. }
  21.  
  22. int main() {
  23.   int players = 430;
  24.   int last = 7158800;
  25.  
  26.   // zero init score vector
  27.   std::vector<long> scores(players, 0);
  28.  
  29.   // init marbles, linked list
  30.   std::list<int> marbles = {0};
  31.  
  32.   // the next to insert, the head iterator
  33.   int i = 1;
  34.   auto h = marbles.begin();
  35.   while (true) {
  36.     // debug print too
  37.     if (i % 71588 == 0) {
  38.       std::cout << "current i " << i << " highest score " << highest(scores)
  39.                 << std::endl;
  40.     }
  41.  
  42.     auto score = (i % 23) == 0;
  43.  
  44.     if (score) {
  45.       auto pi = i % players; // player index
  46.       auto ps = scores[pi];  // previous score
  47.  
  48.       // find anti-closewise by 7, remove that value
  49.       for (int ir = 0; ir < 7; ir++) {
  50.         if (h == marbles.begin()) {
  51.           h = marbles.end();
  52.           h--; // one pass end so --
  53.         } else {
  54.           h--;
  55.         }
  56.       }
  57.       auto v = *h;
  58.       scores[pi] = ps + i + v;
  59.       // Iterator following the last removed element.
  60.       // If pos refers to the last element, then the end() iterator is returned.
  61.       h = marbles.erase(h);
  62.       if (h == marbles.end()) {
  63.         h = marbles.begin();
  64.       }
  65.     } else {
  66.       for (int ia = 0; ia < 2; ia++) {
  67.         if (h == marbles.end()){
  68.           h = marbles.begin();
  69.           h++; // insert before pos
  70.         }else {
  71.           h++;
  72.         }
  73.       }
  74.       h = marbles.insert(h, i); // return inserted pos
  75.     }
  76.     // print_marbles(marbles);
  77.     if (i > last) {
  78.       break;
  79.     }
  80.     i++;
  81.     continue;
  82.   }
  83.  
  84.   // print_marbles(marbles);
  85.   std::cout << "current i " << i << " highest score " << highest(scores)
  86.             << std::endl;
  87.   return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement