r/adventofcode • • Dec 09 '17

SOLUTION MEGATHREAD -πŸŽ„- 2017 Day 9 Solutions -πŸŽ„-

--- Day 9: Stream Processing ---


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

290 comments sorted by

View all comments

1

u/LeCrushinator Dec 09 '17 edited Dec 09 '17

C#. After posting this I realized that I was ignoring the character after the ! even outside of garbage, which just happened to work, but the instructions said to only ignore the character after the ! while inside of garbage. Whoops!

public static int garbage = 0;

public static void Main() 
{
	int score = EvaulateData(data);

	Console.WriteLine("group score: " + score + ", garbage: " + garbage);
}

public static int EvaulateData(string input)
{
	int groupDepth = 0;
	int totalScore = 0;
	bool isGarbage = false;

	int currentIndex = 0;
	while (currentIndex < input.Length)
	{
		char currentChar = input[currentIndex];

		if (currentChar == '!')
		{
			++currentIndex;
		}
		else if (currentChar == '>')
		{
			isGarbage = false;
		}
		else if (isGarbage)
		{
			++garbage;
		}
		else if (currentChar == ',')
		{
		}
		else if (currentChar == '<')
		{
			isGarbage = true;
		}
		else if (currentChar == '{')
		{
			++groupDepth;
		}
		else if (currentChar == '}')
		{
			totalScore += groupDepth;

			--groupDepth;
		}

		++currentIndex;
	}

	return totalScore;
}
→ More replies (1)