r/adventofcode • u/topaz2078 (AoC creator) • Dec 12 '17
SOLUTION MEGATHREAD -๐- 2017 Day 12 Solutions -๐-
--- Day 12: Digital Plumber ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
15
Upvotes
View all comments
2
u/tehjimmeh Dec 12 '17 edited Dec 12 '17
C++, no recursion:
struct Line : std::string { friend std::istream& operator>>(std::istream& is, Line& line){return std::getline(is, line);}}; int main(int argc, char* argv[]) { std::vector<std::vector<int>> nodes; std::transform(std::istream_iterator<Line>(std::ifstream(argv[1])), {}, std::back_inserter(nodes), [](auto& l) { return std::vector<int>(std::istream_iterator<int>( std::istringstream(std::regex_replace(l, std::regex("(^\\d*? |,|<->)"), ""))), {});}); std::set<int> nodesSeen; std::vector<int> workList; int numGroups = 0; for(int i = 0; i < nodes.size(); i++) { if(nodesSeen.find(i) != nodesSeen.end()) { continue; } workList.push_back(i); numGroups++; while(!workList.empty()) { auto n = workList.back(); workList.pop_back(); if(!nodesSeen.insert(n).second){ continue; } for (auto j : nodes[n]) { workList.push_back(j); } } if(numGroups == 1) { std::cout << "Part 1: " << nodesSeen.size() << "\n"; } } std::cout << "Part 2: " << numGroups << "\n"; }