Advertisement
Guest User

--- Day 15: Rambunctious Recitation ---

a guest
Dec 15th, 2020
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.13 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iterator>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <string>
  7. #include <vector>
  8. #include <map>
  9.  
  10. void PrintAnswer(int turn, int num)
  11. {
  12.     std::cout << "Turn " << turn << " - Spoken: " << num << std::endl;
  13. }
  14.  
  15. int main()
  16. {
  17.     std::string text;
  18.     std::ifstream ifs;
  19.     ifs.open("data.txt", std::ifstream::in);
  20.  
  21.     std::vector<int> order;
  22.     while (getline(ifs, text, ','))
  23.     {
  24.         order.push_back(atoi(text.c_str()));
  25.     }
  26.  
  27.     int num = 0;
  28.     int turn = 0;
  29.     int lastNum = -1;
  30.     std::map<int, int> number;
  31.     std::map<int, std::pair<int, int>> last;
  32.     while (turn < 30000000)
  33.     {
  34.         num = 0;
  35.         turn++;
  36.  
  37.         if (turn > order.size())
  38.         {
  39.             if (number[lastNum] > 1) //if spoken before, next number is difference between last turn and last time spoken.
  40.             {
  41.                 num = last[lastNum].first - last[lastNum].second;
  42.             }
  43.             //if last number is new, next number spoken is 0
  44.         }
  45.         else //starting numbers
  46.         {
  47.             num = order[turn - 1];
  48.         }
  49.  
  50.         number[num]++;
  51.         std::swap(last[num].first, last[num].second);
  52.         last[num].first = turn;
  53.         lastNum = num;
  54.     }
  55.    
  56.     PrintAnswer(turn, num);
  57.  
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement