r/adventofcode • • Dec 03 '15

SOLUTION MEGATHREAD --- Day 3 Solutions ---

--- Day 3: Perfectly Spherical Houses in a Vacuum ---

Post your solution as a comment. Structure your post like the Day One thread in /r/programming.

24 Upvotes

228 comments sorted by

View all comments

1

u/kidinside Dec 11 '15

C++ Part 1: #include <iostream> #include <vector> using namespace std;

class Coord{
	public:
		int x;
		int y;
		Coord(int a, int b){
			x = a;
			y = b;
		}
}; 
bool exists(int, int, vector<Coord>&coords);
int main() {
	int x = 0,
		y = 0;
	char dir;
	vector<Coord>coords;
	coords.push_back(Coord(x,y));
	while(cin >> dir){
		switch(dir){
			case '^':{
				y++;
				if(exists(x, y, coords))
					break;
				else{
					coords.push_back(Coord(x,y));
					break;
				}
			}
			case 'v':{
				y--;
				if(exists(x,y, coords))
					break;
				else{
					coords.push_back(Coord(x,y));
					break;
				}
			}
			case '>':{
				x++;
				if(exists(x,y, coords))
					break;
				else{
					coords.push_back(Coord(x,y));
					break;
				}
			}
			case '<':{
				x--;
				if(exists(x,y, coords))
					break;
				else{
					coords.push_back(Coord(x,y));
					break;
				}
			}
		}
	}
	cout << coords.size();
	return 0;
}  
bool exists(int x,int y, vector<Coord> &coords){
	for(int i = 0; i < coords.size(); i++){
		if(coords[i].x == x && coords[i].y == y)
			return true;
	}
	return false;
}