Advertisement
kpvw

Untitled

Dec 20th, 2022
1,055
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <unordered_map>
  6.  
  7. int64_t modulo(int64_t a, int64_t b) { return a >= 0 ? a % b : (b - std::abs(a % b)) % b; }
  8.  
  9. int main() {
  10.   std::ifstream input("input.txt");
  11.  
  12.   std::vector<int64_t> list{};
  13.   std::vector<size_t> indices{};
  14.  
  15.   std::string line;
  16.   while (std::getline(input, line)) {
  17.     int n = std::stoi(line);
  18.     indices.push_back(list.size());
  19.     list.push_back(n);
  20.   }
  21.  
  22.   int64_t decryption_key = 811589153;
  23.  
  24.   for (size_t i = 0; i < list.size(); i++) {
  25.     list[i] *= decryption_key;
  26.   }
  27.  
  28.   //for (auto i : indices) {
  29.   //  std::cout << list[i] << " ";
  30.   //}
  31.   //std::cout << std::endl;
  32.  
  33.   for (size_t k = 0; k < 10; k++) {
  34.     for (size_t i = 0; i < list.size(); i++) {
  35.       int64_t from = std::find(indices.begin(), indices.end(), i) - indices.begin();
  36.       indices.erase(indices.begin() + from);
  37.       int64_t to = modulo(from + list[i], indices.size());
  38.       if (to == 0) {
  39.         indices.push_back(i);
  40.       }
  41.       else {
  42.         indices.insert(indices.begin() + to, i);
  43.       }
  44.     }
  45.     //for (auto i : indices) {
  46.     //  std::cout << list[i] << " ";
  47.     //}
  48.     //std::cout << std::endl;
  49.   }
  50.  
  51.   int answer = 0;
  52.   size_t zero_index = std::find(indices.begin(), indices.end(), std::find(list.begin(), list.end(), 0) - list.begin()) - indices.begin();
  53.   for (size_t i = 1; i <= 3; i++) {
  54.     size_t index = zero_index + 1000 * i;
  55.     index = modulo(index, indices.size());
  56.     std::cout << list[indices[index]] << std::endl;
  57.     answer += list[indices[index]];
  58.   }
  59.  
  60.   std::cout << "Part 2: " << answer << std::endl;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement