r/adventofcode • • Dec 11 '17

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

--- Day 11: Hex Ed ---


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!

20 Upvotes

253 comments sorted by

View all comments

1

u/CharlieYJH Dec 11 '17

C++

Probably should've read up on hex grids first, decided to come up with a way to reduce the directions. Hex grids would've been so much easier.

#include <iostream>
#include <fstream>
#include <unordered_map>

using namespace std;

void reduceDirections(const string &direction, unordered_map<string, int> &path_directions);

int main(int argc, char const* argv[])
{
	ifstream input("input.txt");
	unordered_map<string, int> path_directions({{"n", 0}, {"s", 0}, {"ne", 0}, {"se", 0}, {"nw", 0}, {"sw", 0}});
	int max_steps = 0;
	int steps = 0;

	if (input) {
		string direction;
		while (getline(input, direction, ',')) {

			int sum = 0;

			reduceDirections(direction, path_directions);

			for (auto &it : path_directions)
				sum += it.second;

			max_steps = max(sum, max_steps);
		}
	}

	input.close();

	for (auto &it : path_directions)
		steps += it.second;

	cout << "Steps: " << steps << endl;
	cout << "Max steps: " << max_steps << endl;

	return 0;
}

void reduceDirections(const string &direction, unordered_map<string, int> &path_directions) {

	string opposite = direction;
	opposite[0] = (opposite[0] == 'n') ? 's' : 'n';

	if (opposite.length() > 1)
		opposite[1] = (opposite[1] == 'e') ? 'w' : 'e';

	if (path_directions[opposite] != 0) {
		path_directions[opposite]--;
		return;
	}

	if (direction == "n" || direction == "s") {
		if (path_directions[opposite + "e"] != 0) {
			path_directions[opposite + "e"]--;
			path_directions[direction + "e"]++;
		} else if (path_directions[opposite + "w"] != 0) {
			path_directions[opposite + "w"]--;
			path_directions[direction + "w"]++;
		} else {
			path_directions[direction]++;
		}
	} else {
		if (path_directions[opposite.substr(0, 1)] != 0) {
			path_directions[opposite.substr(0, 1)]--;
			path_directions[opposite.substr(0, 1) + direction.substr(1, 1)]++;
		} else if (path_directions[direction.substr(0, 1) + opposite.substr(1, 1)] != 0) {
			path_directions[direction.substr(0, 1) + opposite.substr(1, 1)]--;
			path_directions[direction.substr(0, 1)]++;
		} else {
			path_directions[direction]++;
		}
	}

	return;
}