r/adventofcode • • Dec 13 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 13 Solutions -๐ŸŽ„-

--- Day 13: Packet Scanners ---


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!

16 Upvotes

199 comments sorted by

View all comments

1

u/Kenira Dec 13 '17

C++

Taking 65ms after a few improvements which i am quite happy with

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 delay = -1;

while (true)
{
	int last_ind = 0;
	++delay;
	int current_severity = 0;
	for (int pos = 0; pos <= vint.back()[0]; pos++)
	{
		int ind = -1;
		for (int i = last_ind; i < vint.size(); i++)
		{
			if (vint[i][0] == pos)
			{
				ind = i;
				last_ind = ind;
			}
			else if (vint[i][0] > pos)
			{
				break;
			}
		}
		if (ind >= 0)
		{
			if (((vint[ind][0] + delay) % (2 * (vint[ind][1] - 1))) == 0)
			{
				current_severity += vint[ind][0] * vint[ind][1];
				if (delay != 0)
				{
					current_severity += 1;
					break;
				}
			}
		}
	}
	if (current_severity == 0)
	{
		break;
	}
	if (delay == 0)
	{
		cout << "Severity with no delay: " << current_severity << endl;
	}
}

cout << "Delay to not get caught: " << delay << endl;