Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <unordered_map>
- int64_t modulo(int64_t a, int64_t b) { return a >= 0 ? a % b : (b - std::abs(a % b)) % b; }
- int main() {
- std::ifstream input("input.txt");
- std::vector<int64_t> list{};
- std::vector<size_t> indices{};
- std::string line;
- while (std::getline(input, line)) {
- int n = std::stoi(line);
- indices.push_back(list.size());
- list.push_back(n);
- }
- int64_t decryption_key = 811589153;
- for (size_t i = 0; i < list.size(); i++) {
- list[i] *= decryption_key;
- }
- //for (auto i : indices) {
- // std::cout << list[i] << " ";
- //}
- //std::cout << std::endl;
- for (size_t k = 0; k < 10; k++) {
- for (size_t i = 0; i < list.size(); i++) {
- int64_t from = std::find(indices.begin(), indices.end(), i) - indices.begin();
- indices.erase(indices.begin() + from);
- int64_t to = modulo(from + list[i], indices.size());
- if (to == 0) {
- indices.push_back(i);
- }
- else {
- indices.insert(indices.begin() + to, i);
- }
- }
- //for (auto i : indices) {
- // std::cout << list[i] << " ";
- //}
- //std::cout << std::endl;
- }
- int answer = 0;
- size_t zero_index = std::find(indices.begin(), indices.end(), std::find(list.begin(), list.end(), 0) - list.begin()) - indices.begin();
- for (size_t i = 1; i <= 3; i++) {
- size_t index = zero_index + 1000 * i;
- index = modulo(index, indices.size());
- std::cout << list[indices[index]] << std::endl;
- answer += list[indices[index]];
- }
- std::cout << "Part 2: " << answer << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement