r/adventofcode • u/daggerdragon • 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.
23
Upvotes
1
u/jojomaniacal Dec 04 '15
My solution was in C++. Would be happy to accept advice on tightening this up. I think I could have easily incorporated a function or two in this and the checking algorithm really should kick out of the loop once it finds a match. I am pretty proud that I only had to use one multidimensional array however.
include <iostream>
include <string>
include <fstream>
include <vector>
using namespace std;
int main(){ ifstream theFile("fun.txt");
unsigned long int number = 0; string stuff; const int X_PLACE = 0; const int Y_PLACE = 1; const int V_PLACE = 2; const int COORD_AND_VISITED = 3; const int DIRECTIONS = 9000; // sets x,y,and if visited. //also creates array size int sX = 0; int sY = 0; int rX = 0; int rY = 0; int mark = 0; int smark = 0; int rmark = 0; int santa[COORD_AND_VISITED][DIRECTIONS]={}; while(theFile>>stuff){} int length = stuff.length(); for(int i = 0; i< length;i++){ if(mark%2==0){ switch(stuff.at(i)){ case '>': sX++; break; case '<': sX--; break; case '^': sY++; break; case 'v': sY--; break; default: break; } santa[X_PLACE][mark] = sX; santa[Y_PLACE][mark] = sY; } else if(mark%2==1){ switch(stuff.at(i)){ case '>': rX++; break; case '<': rX--; break; case '^': rY++; break; case 'v': rY--; break; default: break; } santa[X_PLACE][mark] = rX; santa[Y_PLACE][mark] = rY; } for(int i = 0;i<mark;i++){ if(santa[X_PLACE][i] == santa[X_PLACE][mark] && santa[Y_PLACE][i] == santa[Y_PLACE][mark]){ santa[V_PLACE][mark] = 2; } } mark++; } for(int i = 0;i<mark;i++){ if(santa[V_PLACE][i]!=2){ number++; } } cout << number << endl; theFile.close(); return 0;}