Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <iterator>
- #include <fstream>
- #include <sstream>
- #include <string>
- #include <vector>
- #include <map>
- void PrintAnswer(int turn, int num)
- {
- std::cout << "Turn " << turn << " - Spoken: " << num << std::endl;
- }
- int main()
- {
- std::string text;
- std::ifstream ifs;
- ifs.open("data.txt", std::ifstream::in);
- std::vector<int> order;
- while (getline(ifs, text, ','))
- {
- order.push_back(atoi(text.c_str()));
- }
- int num = 0;
- int turn = 0;
- int lastNum = -1;
- std::map<int, int> number;
- std::map<int, std::pair<int, int>> last;
- while (turn < 30000000)
- {
- num = 0;
- turn++;
- if (turn > order.size())
- {
- if (number[lastNum] > 1) //if spoken before, next number is difference between last turn and last time spoken.
- {
- num = last[lastNum].first - last[lastNum].second;
- }
- //if last number is new, next number spoken is 0
- }
- else //starting numbers
- {
- num = order[turn - 1];
- }
- number[num]++;
- std::swap(last[num].first, last[num].second);
- last[num].first = turn;
- lastNum = num;
- }
- PrintAnswer(turn, num);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement