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!

10 Upvotes

186 comments sorted by

View all comments

1

u/CharlieYJH Dec 19 '17 edited Dec 19 '17

C++

Fun puzzle today. Chose to do it recursively.

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

enum {
	UP, DOWN, LEFT, RIGHT
};

bool traverse(vector<string> &maze, int x, int y, int direction, string &collection, int &steps)
{
	if (x < 0 || y < 0 || y >= maze.size() || x >= maze[y].length())
		return false;

	if (maze[y][x] != '|' && maze[y][x] != '-' && maze[y][x] != '+' && !(maze[y][x] >= 'A' && maze[y][x] <= 'Z'))
		return false;

	steps++;

	if (maze[y][x] >= 'A' && maze[y][x] <= 'Z') {
		collection += maze[y][x];
	} else if (maze[y][x] == '+') {
		if (direction == DOWN || direction == UP)
			return traverse(maze, x + 1, y, RIGHT, collection, steps) || traverse(maze, x - 1, y, LEFT, collection, steps);
		else
			return traverse(maze, x, y + 1, DOWN, collection, steps) || traverse(maze, x, y - 1, UP, collection, steps);
	}

	if (direction == DOWN)
		traverse(maze, x, y + 1, direction, collection, steps);
	else if (direction == UP)
		traverse(maze, x, y - 1, direction, collection, steps);
	else if (direction == LEFT)
		traverse(maze, x - 1, y, direction, collection, steps);
	else
		traverse(maze, x + 1, y, direction, collection, steps);

	return true;
}

int main(int argc, char const* argv[])
{
	ifstream infile("input.txt");
	vector<string> maze;
	string collection = "";
	int start;
	int steps = 0;

	if (infile.is_open()) {
		string line;
		while (getline(infile, line))
			maze.push_back(line);
		infile.close();
	} else {
		return 1;
	}

	for (int i = 0; i < maze[0].length(); i++)
		if (maze[0][i] == '|') start = i;

	traverse(maze, start, 0, DOWN, collection, steps);

	cout << "Collection: " << collection << endl;
	cout << "Steps: " << steps << endl;

	return 0;
}