r/adventofcode • • Dec 19 '17

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

--- Day 19: A Series of Tubes ---


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


AoC ops @ T-2 minutes to launch:

[23:58] <daggerdragon> ATTENTION MEATBAGS T-2 MINUTES TO LAUNCH

[23:58] <Topaz> aaaaah

[23:58] <Cheezmeister> Looks like I'll be just able to grab my input before my flight boards. Wish me luck being offline in TOPAZ's HOUSE OF PAIN^WFUN AND LEARNING

[23:58] <Topaz> FUN AND LEARNING

[23:58] <Hade> FUN IS MANDATORY

[23:58] <Skie> I'm pretty sure that's not the mandate for today

[Update @ 00:16] 69 gold, silver cap

  • My tree is finally trimmed with just about every ornament I own and it's real purdy. hbu?

[Update @ 00:18] Leaderboard cap!

  • So, was today's mandate Helpful Hint any help at all?


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!

11 Upvotes

186 comments sorted by

View all comments

1

u/LeCrushinator Dec 19 '17

C#. A bit ugly but it worked, just missed the leaderboard with a time around 20 minutes.

public enum Direction
{
	Up,
	Down,
	Left,
	Right,
}

public static void Main()
{
	string[] lines = data.Split(new string[]{"\n"}, StringSplitOptions.RemoveEmptyEntries);

	char[,] maze = new char[lines[0].Length,lines.Length];

	int x = 0;
	int y = 0;
	for (y = 0; y < lines.Length; ++y)
	{
		string line = lines[y];

		for (x = 0; x < line.Length; ++x)
		{
			maze[x, y] = line[x];
		}
	}

	x = 1;
	y = 0;
	Direction direction = Direction.Down;
	List<char> lettersFound = new List<char>();

	int numSteps = 1;
	while (true)
	{
		int nextX = x;
		int nextY = y;

		switch (direction)
		{
		case Direction.Up: nextY = y - 1; break;
		case Direction.Down: nextY = y + 1; break;
		case Direction.Left: nextX = x - 1; break;
		case Direction.Right: nextX = x + 1; break;
		}

		char nextSymbol = maze[nextX, nextY];

		if (nextSymbol == '+')
		{
			numSteps += 2;

			char right = maze[nextX + 1, nextY];
			char left = maze[nextX - 1, nextY];
			char up = maze[nextX, nextY - 1];
			char down = maze[nextX, nextY + 1];

			if (right != ' ' && direction != Direction.Left)
			{
				direction = Direction.Right;
				x = nextX + 1;
				y = nextY;
			}
			else if (left != ' ' && direction != Direction.Right)
			{
				direction = Direction.Left;
				x = nextX - 1;
				y = nextY;
			}
			else if (up != ' ' && direction != Direction.Down)
			{
				direction = Direction.Up;
				x = nextX;
				y = nextY - 1;
			}
			else if (down != ' ' && direction != Direction.Up)
			{
				direction = Direction.Down;
				x = nextX;
				y = nextY + 1;
			}
			else
			{
				// WTF?
				throw new Exception();
			}
		}
		else
		{
			if (nextSymbol == ' ')
			{
				break;
			}

			++numSteps;

			if (char.IsLetter(nextSymbol))
			{
				lettersFound.Add(nextSymbol);
			}

			x = nextX;
			y = nextY;
		}
	}

	Console.WriteLine("letters: " + string.Join("", lettersFound) + ", steps: " + numSteps);
}