Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <regex>
  5. #include <map>
  6. #include <set>
  7.  
  8. using namespace std;
  9.  
  10. string inpath = "C:\\Users\\vini2003\\Documents\\Advent of Code\\Day 4\\input";
  11.  
  12. map<int, tuple<vector<int>, vector<int>, int>> list;
  13.  
  14. vector<string> storage;
  15.  
  16. set<int> IDs;
  17.  
  18. int ID = 0, minute = 0, tmpOcc = 0;
  19.  
  20. void read() {
  21.     ifstream input(inpath);
  22.     string line;
  23.     while (getline(input, line)) {
  24.         storage.push_back(line);
  25.     }
  26.     sort(storage.begin(), storage.end());
  27.     for (string str : storage) {
  28.         if (regex_match(str, regex(".+#.+"))) {
  29.             smatch matches;
  30.             regex_search(str, matches, regex(".+#([0-9]+).+"));
  31.             IDs.insert(stoi(matches[1]));
  32.             ID = stoi(matches[1]);
  33.         }
  34.         if (regex_match(str, regex(".+00:[0-9]+.+fall.+"))) {
  35.             smatch matches;
  36.             regex_search(str, matches, regex("00:([0-9]+)"));
  37.             get<0>(list[ID]).push_back(stoi(matches[1]));
  38.         }
  39.         if (regex_match(str, regex(".+00:[0-9]+.+wake.+"))) {
  40.             smatch matches;
  41.             regex_search(str, matches, regex("00:([0-9]+)"));
  42.             get<1>(list[ID]).push_back(stoi(matches[1]));
  43.         }
  44.     }
  45.     tuple<int, int> slept = { 0, 0 };
  46.     for (int tmpID : IDs) {
  47.         for (int awake : get<1>(list[tmpID])) {
  48.             get<2>(list[tmpID]) += awake;
  49.         }
  50.         for (int sleep : get<0>(list[tmpID])) {
  51.             get<2>(list[tmpID]) -= sleep;
  52.         }
  53.         if (get<2>(list[tmpID]) > get<1>(slept)) {
  54.             slept = { tmpID, get<2>(list[tmpID]) };
  55.         }
  56.     }
  57.     map<int, int> occur;
  58.     for (int i = 0; i < get<0>(list[get<0>(slept)]).size(); i++) {
  59.         for (int k = get<0>(list[get<0>(slept)])[i]; k < get<1>(list[get<0>(slept)])[i]; k++) {
  60.             occur[k]++;
  61.         }
  62.     }
  63.     for (int i = 0; i < 60; i++) {
  64.         cout << i << "\t" << occur[i] << endl;
  65.         if (occur[i] > tmpOcc) {
  66.             tmpOcc = occur[i];
  67.             minute = i;
  68.         }
  69.     }
  70.     cout << "Slept the most: " << get<0>(slept) << " at " << get<1>(slept) << ", with the most slept-during minute being: " << minute << ", thus the code is: " << get<0>(slept) * minute;
  71. }
  72.  
  73. int main() {
  74.     read();
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement