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!

13 Upvotes

233 comments sorted by

View all comments

Show parent comment

2

u/TheAngryGerman Dec 12 '17

Isn't string to number just stoi(myString)? I definitely spent far too long typing my iterators as well, almost considered a #define for my map, but figured it wasn't worth it...

1

u/Vorlath Dec 12 '17

Ah... forgot about stoi. Was using atoi(str.c_str()). About iterators, so glad for range iteration, but in this case the first one is the group id, so I needed to iterate from the second one so I did longform iteration.

I'm down to under 3 minutes if I used macros, but I doubt I'll remember what they are next time.

void star12F2(const std::string &in_filename)
{
	FileStrings fileStrings;
	std::vector<char> delimeters = { ' ', '\t', '-', '<', '>',/*',',*/ '\n', '\r' /*, '(', ')'*/ };
	readFile(in_filename, delimeters, fileStrings);

	xmap<int, xvec<int>> a;

	xforall(line, fileStrings)
	{
		auto itr = line.xb();
		int n = xnum(itr);
		++itr;
		xiterate(itr, line.xe())
			a[n].xp(xnum(itr));
	}

	int cnt = 0;

	while (!a.empty())
	{
		xvec<int> v;
		v.xp(a.xb()->x1);
		int index = 0;

		for (;;)
		{
			if (index >= v.xs())
				break;

			int n = v[index++];
			for (auto &&m : a[n])
			{
				if (!xexists(v, m))
					v.xp(m);
			}
			a.erase(n);
		}
		cnt++;
	}
	std::cout << "n: " <<cnt  << std::endl;
}