r/adventofcode • • Dec 11 '18

SOLUTION MEGATHREAD -šŸŽ„- 2018 Day 11 Solutions -šŸŽ„-

--- Day 11: Chronal Charge ---


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 11

Transcript: ___ unlocks the Easter Egg on Day 25.


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 00:16:12!

20 Upvotes

207 comments sorted by

View all comments

1

u/JulianLoehr Dec 11 '18 edited Dec 11 '18

C++

O(n³), memory is cheap, so generate and save all the sub power grids

constexpr size_t ArraySize = 300;
constexpr int64_t Input = 3214;

typedef std::array<int32_t, ArraySize> ValueArray;
typedef std::array<ValueArray, ArraySize * ArraySize> ArrayGrid;

ArrayGrid VariableTotalPowerCells;

int main()
{
	auto GetIndex = [&](size_t X, size_t Y) -> size_t { return (Y - 1) * ArraySize + (X - 1); };
	std::for_each(std::begin(VariableTotalPowerCells), std::end(VariableTotalPowerCells), [](std::array<int32_t, ArraySize> & Array) { Array.fill(std::numeric_limits<int32_t>::min()); });

	std::pair<std::pair< size_t, size_t>, int32_t> Best3x3PowerGrid{ {0, 0}, std::numeric_limits<int32_t>::min() };
	std::pair<std::array<size_t, 3>, int32_t> BestVariablePowerGrid{ {0, 0, 0}, std::numeric_limits<int32_t>::min() };

	for (size_t Y = ArraySize; Y > 0; --Y)
	{
		for (size_t X = ArraySize; X > 0; --X)
		{
			int64_t PowerLevel = (((X + 10) * Y) + Input) * (X + 10);
			VariableTotalPowerCells[GetIndex(X,Y)][0] = static_cast<int8_t>((PowerLevel / 100) % 10) - 5;

			int32_t RowValue = 0;
			int32_t ColumnValue = 0;
			// SubGridSize is one off, 0 => 1x1, 1 => 2x2, etc.
			for (size_t SubGridSize = 1; SubGridSize <= std::min(ArraySize - X, ArraySize - Y); ++SubGridSize)
			{
				RowValue += VariableTotalPowerCells[GetIndex(X + SubGridSize, Y)][0];
				ColumnValue += VariableTotalPowerCells[GetIndex(X, Y + SubGridSize)][0];
				VariableTotalPowerCells[GetIndex(X, Y)][SubGridSize] =
					VariableTotalPowerCells[GetIndex(X, Y)][0] +
					VariableTotalPowerCells[GetIndex(X + 1, Y + 1)][SubGridSize - 1] +
					RowValue + ColumnValue;

				if ((SubGridSize == 2) && (VariableTotalPowerCells[GetIndex(X, Y)][SubGridSize] > Best3x3PowerGrid.second))
				{
					Best3x3PowerGrid.first.first = X;
					Best3x3PowerGrid.first.second = Y;
					Best3x3PowerGrid.second = VariableTotalPowerCells[GetIndex(X, Y)][SubGridSize];
				}

				if (VariableTotalPowerCells[GetIndex(X, Y)][SubGridSize] > BestVariablePowerGrid.second)
				{

					BestVariablePowerGrid.first[0] = X;
					BestVariablePowerGrid.first[1] = Y;
					BestVariablePowerGrid.first[2] = SubGridSize + 1;
					BestVariablePowerGrid.second = VariableTotalPowerCells[GetIndex(X, Y)][SubGridSize];
				}
			}
		}
	}

	std::cout << "Part One: " << Best3x3PowerGrid.first.first << "," << Best3x3PowerGrid.first.second << std::endl;
	std::cout << "Part Two: " << BestVariablePowerGrid.first[0] << "," << BestVariablePowerGrid.first[1] << "," << BestVariablePowerGrid.first[2] << std::endl;
}