Advertisement
Guest User

Advent Of Code Day 12 solution C++

a guest
Dec 12th, 2017
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <iostream>
  6. #include <chrono>
  7. #include <functional>
  8. #include <algorithm>
  9. #include <regex>
  10. #include <map>
  11.  
  12. using namespace std;
  13. using std::regex;
  14.  
  15. struct Node {
  16.     int name;
  17.     bool visited = false;
  18.     int groupNr = 0;
  19.  
  20.     bool operator<(const Node& p) const
  21.     {
  22.         return name < p.name;
  23.     }
  24.  
  25.     std::vector<int> children;
  26. };
  27.  
  28. void visit_nodes(map<int, Node>& pipeline, vector<int>& nodes, const int group_nr)
  29. {
  30.     for (auto node : nodes) {
  31.         if (!pipeline[node].visited) {
  32.             pipeline[node].visited = true;
  33.             pipeline[node].groupNr = group_nr;
  34.             visit_nodes(pipeline, pipeline[node].children, group_nr);
  35.         }
  36.     }
  37. }
  38.  
  39. int _tmain(int argc, _TCHAR* argv[])
  40. {
  41.  
  42.     ifstream myfile("Input.txt");
  43.  
  44.     string line;
  45.     map<int, Node> pipeline;
  46.  
  47.     if (myfile.is_open()) {
  48.  
  49.         const regex base_regex("(\\d+) <-> ([0-9, ]+)");
  50.         const regex children_regex("(, )");
  51.  
  52.         smatch pieces_match;
  53.  
  54.         while (getline(myfile, line)) {
  55.             if (!line.empty()) {
  56.                 if (regex_match(line, pieces_match, base_regex)) {
  57.                     Node node;
  58.  
  59.                     node.name = stoi(pieces_match[1]);
  60.  
  61.                     if (pieces_match[2].matched) {
  62.                         string list = pieces_match[2];
  63.  
  64.                         for_each(std::sregex_token_iterator(list.begin(), list.end(), children_regex, -1), std::sregex_token_iterator(),
  65.                             [&node](const string& ch) { node.children.push_back(stoi(ch)); });
  66.                     }
  67.                     pipeline[node.name] = node;
  68.                 }
  69.             }
  70.         }
  71.     }
  72.     else {
  73.         cout << "cannot open file " << endl;
  74.         return 0;
  75.     }
  76.  
  77.     const auto begin = std::chrono::high_resolution_clock::now();
  78.  
  79.     auto groupNr = 0;
  80.  
  81.     for (auto i = 0; i < pipeline.size(); i++) {
  82.         if (pipeline[i].groupNr == 0) {
  83.             groupNr++;
  84.             pipeline[i].visited = true;
  85.             pipeline[i].groupNr = groupNr;
  86.             visit_nodes(pipeline, pipeline[i].children, groupNr);
  87.         }
  88.     }
  89.  
  90.     auto number = count_if(pipeline.begin(), pipeline.end(), [](pair<int, Node> it) { return (it.second.groupNr == 1); });
  91.  
  92.     const auto end2 = std::chrono::high_resolution_clock::now();
  93.  
  94.     cout << "First part: " << number << endl;
  95.     cout << "Second part: " << groupNr << endl;
  96.  
  97.     std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(end2 - begin).count() << " ns for both parts" << std::endl;
  98.  
  99.     cin.ignore();
  100.  
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement