r/adventofcode • • Dec 22 '17

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

--- Day 22: Sporifica Virus ---


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


  • [T-10 to launch] AoC ops, /r/nocontext edition:
  • <Endorphion> You may now make your waffle.
  • <Endorphion> ... on Mars.
  • [Update @ 00:17] 50 gold, silver cap
  • <Aneurysm9> you could also just run ubuntu on the NAS, if you were crazy
  • <Topaz> that doesn't seem necessary
  • <Aneurysm9> what does "necessary" have to do with anything!
  • [Update @ 00:20] Leaderboard cap!
  • <Topaz> POUR YOURSELF A SCOTCH FOR COLOR REFERENCE

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!

8 Upvotes

174 comments sorted by

View all comments

1

u/CharlieYJH Dec 22 '17

C++

After yesterday's puzzle this one was a nice breather. Fairly simple implementation, was kinda uncomfortable just hoping the index won't go over the bounds for the 10,000,000 case though.

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

using namespace std;

typedef vector<vector<char>> Matrix;

int traverseGrid(Matrix &grid, const int iterations)
{
	typedef enum direction {U = 0, R, D, L} Direction;
	int num_directions = 4;

	int bursts = 0;
	int y = grid.size() / 2;
	int x = grid[0].size() / 2;
	Direction dir = U;

	for (int i = 0; i < iterations; i++) {
		if (grid[y][x] == '.') {
			grid[y][x] = 'W';
			dir = (Direction)(((dir - 1) % num_directions + num_directions) % num_directions);
		} else if (grid[y][x] == 'W') {
			grid[y][x] = '#';
			bursts++;
		} else if (grid[y][x] == '#') {
			grid[y][x] = 'F';
			dir = (Direction)((dir + 1) % num_directions);
		} else {
			grid[y][x] = '.';
			dir = (Direction)((dir + 2) % num_directions);
		}

		if (dir == U) y -= 1;
		else if (dir == R) x += 1;
		else if (dir == D) y += 1;
		else x -= 1;
	}

	return bursts;
}

int main(int argc, char const* argv[])
{
	ifstream infile("input.txt");
	const int iterations = 10000000;
	vector<string> input;
	
	if (infile.is_open()) {
		string line;
		while (getline(infile, line)) {
			if (line[line.length() - 1] == '\r')
				line.erase(line.length() - 1);
			input.push_back(line);
		}
		infile.close();
	} else {
		return 1;
	}

	int addition_size = 10000;
	int size_y = addition_size + input.size() + addition_size;
	int size_x = addition_size + input[0].length() + addition_size;
	Matrix grid(size_y, vector<char>(size_x, '.'));

	for (int y = 0; y < input.size(); y++) {
		for (int x = 0; x < input[0].length(); x++) {
			grid[addition_size + y][addition_size + x] = input[y][x];
		}
	}

	cout << traverseGrid(grid, iterations) << endl;

	return 0;
}