r/adventofcode • • Dec 06 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 6 Solutions -🎄-

--- Day 6: Chronal Coordinates ---


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.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 6

Transcript:

Rules for raising a programmer: never feed it after midnight, never get it wet, and never give it ___.


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 at 0:26:52!

32 Upvotes

387 comments sorted by

View all comments

1

u/JulianLoehr Dec 07 '18

C++

constexpr uint32_t TotalDistanceLimit = 10000;

struct Area 
{
	int32_t X, Y, Size;
	bool Infinite;
};

int main()
{
	const StringVectorVector File = GetFileLineParts("Input.txt");
	std::vector<Area> Areas;
	std::vector<std::pair<Area *, uint32_t>> Distances;
	int32_t MinX = 0;
	int32_t MaxX = 0;
	int32_t MinY = 0;
	int32_t MaxY = 0;
	int32_t PartTwoAreaSize = 0;

	std::transform(
		std::begin(File),
		std::end(File),
		std::back_inserter(Areas),
		[&](const auto & Line)
	{
		int32_t X = std::stoul(Line[0]);
		int32_t Y = std::stoul(Line[1]);

		MinX = std::min(MinX, X);
		MaxX = std::max(MaxX, X);
		MinY = std::min(MinY, Y);
		MaxY = std::max(MaxY, Y);

		return Area{ X, Y, 0, false };
	});

	Distances.reserve(Areas.size());

	for (int32_t Y = MinY; Y <= MaxY; ++Y)
	{
		for (int32_t X = MinX; X <= MaxX; ++X)
		{
			Distances.clear();

			for (Area & Start : Areas)
				Distances.push_back({ &Start, static_cast<uint32_t>(std::abs(X - Start.X) + std::abs(Y - Start.Y))});

			std::sort(std::begin(Distances), std::end(Distances), [](const auto & r, const auto & l) { return r.second < l.second; });
			uint32_t TotalDistances = std::accumulate(std::begin(Distances), std::end(Distances), 0, [](const uint32_t Value, const auto & Distance) { return Value + Distance.second; });

			if (TotalDistances < TotalDistanceLimit)
				++PartTwoAreaSize;

			// Same distance to multiple Starting Points, therefore tied
			if (Distances[0].second == Distances[1].second)
				continue;

			Area & Closest = *Distances[0].first;

			++Closest.Size;
			if ((!Closest.Infinite) && ((X == MinX) || (X == MaxX) || (Y == MinY) || (Y == MaxY)))
				Closest.Infinite = true;
		}
	}

	std::sort(std::begin(Areas), std::end(Areas), [](const Area & r, const Area & l) { return r.Size > l.Size; });
	Area & Largest = *std::find_if(std::begin(Areas), std::end(Areas), [](const Area & k) { return !k.Infinite; });

	std::cout << "Part One: " << Largest.Size << std::endl;
	std::cout << "Part Two: " << PartTwoAreaSize << std::endl;
}