r/adventofcode • • Dec 03 '17

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

--- Day 3: Spiral Memory ---


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!

21 Upvotes

297 comments sorted by

View all comments

3

u/Krakhan Dec 03 '17 edited Dec 03 '17

C# solution. I noticed the patterns like others did of having to increment the step amount in a direction every 2 times, and I didn't see the mathematical way of doing Part A. So, I just iterated through the spiral. At least for part B I could basically reuse that code and just compute the sum values for each part as well.

class Program
{
	static int Day3SpiralMemoryPart1(int n)
	{
		if (n == 1) return 0;

		var x = 0;
		var y = 0;

		var stepCount = 1; // Initial step amount.
		var stepCountChange = true; // Change when true.
		var direction = 0; // right, up, left, down

		// Get the x,y coordinate for each step of i. 
		for (var i = 1;;)
		{
			for (var j = 0; j < stepCount; j += 1)
			{
				// Take a step
				switch (direction)
				{
					case 0:
						// right
						x += 1;
						break;
					case 1: 
						// up
						y += 1;
						break;

					case 2:
						// left
						x -= 1;
						break;

					case 3:
						// down
						y -= 1;
						break;
					default:
						break;
				}
				
				// Needs to be incremented here after we take a step.
				// Then we check the outer loop condition here, and so then jump out if needed.
				// The ghost of Djikstra will probably haunt me for a bit now...~
				i += 1;

				if (i == n) goto EndOfLoop;
			} 

			direction = (direction + 1) % 4;
			stepCountChange = !stepCountChange;
			if (stepCountChange) stepCount += 1;
		}
EndOfLoop:
		var l1distance = Math.Abs(x) + Math.Abs(y);

		System.Diagnostics.Debug.WriteLine("f({0}) = ({1},{2}), |f({0})| = {3}", n, x, y, l1distance);

		return l1distance;
	}

	static int Day3SpiralMemoryPart2(int n)
	{
		if (n == 1) return 1;

		var x = 0;
		var y = 0;

		var stepCount = 1; // Initial step amount.
		var stepCountChange = true; // Change when true.
		var direction = 0;
		var values = new Dictionary<string, int>();

		values["0,0"] = 1;

		for (;;)
		{
			for (var j = 0; j < stepCount; j += 1)
			{
				// Take a step
				switch (direction)
				{
					case 0:
						// right
						x += 1;
						break;
					case 1:
						// up
						y += 1;
						break;

					case 2:
						// left
						x -= 1;
						break;

					case 3:
						// down
						y -= 1;
						break;
					default:
						break;
				}

				// Determine sum of neighbours for value of current location.
				var sum = 0;
				var val = 0;

				if (values.TryGetValue(string.Format("{0},{1}", x + 1, y), out val)) sum += val;
				if (values.TryGetValue(string.Format("{0},{1}", x + 1, y + 1), out val)) sum += val;
				if (values.TryGetValue(string.Format("{0},{1}", x, y + 1), out val)) sum += val;
				if (values.TryGetValue(string.Format("{0},{1}", x - 1, y + 1), out val)) sum += val;
				if (values.TryGetValue(string.Format("{0},{1}", x - 1, y), out val)) sum += val;
				if (values.TryGetValue(string.Format("{0},{1}", x - 1, y - 1), out val)) sum += val;
				if (values.TryGetValue(string.Format("{0},{1}", x, y - 1), out val)) sum += val;
				if (values.TryGetValue(string.Format("{0},{1}", x + 1, y - 1), out val)) sum += val;

				// Check here to see if the sum exceeds our input. Otherwise, store the sum computed and continue.
				if (sum > n) return sum;
				values[string.Format("{0},{1}", x, y)] = sum;
			}

			direction = (direction + 1) % 4;
			stepCountChange = !stepCountChange;
			if (stepCountChange) stepCount += 1;
		}
	}

	static void Main(string[] args)
	{
		var day3PuzzleInput = 277678;

		Console.WriteLine(Day3SpiralMemoryPart1(day3PuzzleInput));
		Console.WriteLine(Day3SpiralMemoryPart2(day3PuzzleInput));

		Console.WriteLine("Press Any Key To Continue...");
		Console.ReadKey();
	}
}

3

u/maxxori Dec 03 '17 edited Dec 03 '17

This one was quite useful in error checking my original workings, thanks!

As it turns out I made a silly mistake in the loop that caused it all to go whackey. Once I tracked it down, it worked great.

2

u/ZoekDribbel Dec 03 '17

I also needed a goto to jump out of my double nested loop. First time (legitimately) using goto in c#. I feel dirty now.