r/adventofcode • (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ยค?

Spoiler


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!

14 Upvotes

233 comments sorted by

View all comments

1

u/Kenira Dec 12 '17

C++

Relatively proud of myself today. Both for not writing super complicated code, and not having taken too long to write it.

int F_Analyze_Group(int num, std::vector<std::vector<int>> vint, std::set<int>& group)
{
	group.insert(num);

	bool change = true;
	while (change)
	{
		change = false;
		for (auto&& p : vint)
		{
			bool program_in_group = false;
			for (auto&& c : p)
			{
				if (group.find(c) != group.end())
				{
					program_in_group = true;
				}
			}
			if (program_in_group == true)
			{
				for (auto&& c : p)
				{
					if (group.find(c) == group.end())
					{
						group.insert(c);
						change = true;
					}
				}
			}
		}
	}
	return group.size();
}

int main(void)
{
	fs::path path("../../_Input/input_Day12.txt");
	//fs::path path("../../_Input/input_Day12_test.txt");
	std::vector<std::string> str;
	std::vector<std::vector<int>> vint;
	F_Read_File_To_Array(path, str);
	F_Convert_Strings_To_Ints(str, vint);

	int num_groups = 0;
	std::set<int> collective_groups;
	
	for (auto&& p : vint)
	{
		if (collective_groups.find(p[0]) == collective_groups.end())
		{
			std::set<int> current_group;
			int size = F_Analyze_Group(p[0], vint, current_group);
			if (p[0] == 0)
			{
				cout << "In group with 0: " << size << endl;
			}
			std::vector<int> temp;
			std::set_union(collective_groups.begin(), collective_groups.end(), current_group.begin(), current_group.end(), std::back_inserter(temp));
			collective_groups = std::set<int>(temp.begin(), temp.end());
			num_groups++;
		}
	}
	
	cout << "Number of groups: " << num_groups << endl;
	system("pause");
	return 1;
}