Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // adventofcode
- //
- // Created by Jonathan Hirsch on 1/26/17.
- // Copyright © 2017 Jonathan Hirsch. All rights reserved.
- //
- #include <iostream>
- #include <sstream>
- #include <stdlib.h>
- #include <stdio.h>
- using namespace std;
- int poss [948][2]; // 948 = total distance traveled. Modify as needed. Code will print out total distance travelled.
- int arrctr = 0;
- int distanceT = 0;
- int incrX;
- int incrY;
- int posX;
- int posY;
- char curDir;
- void moveToPos(char curDir, int dist);
- int main()
- {
- incrX = 0;
- incrY = 0;
- posX = 0;
- posY = 0;
- curDir = 'N';
- string str = "R3, L5, R2, L2, R1, L3, R1, R3, L4, R3, L1, L1, R1, L3, R2, L3, L2, R1, R1, L1, R4, L1, L4, R3, L2, L2, R1, L1, R5, R4, R2, L5, L2, R5, R5, L2, R3, R1, R1, L3, R1, L4, L4, L190, L5, L2, R4, L5, R4, R5, L4, R1, R2, L5, R50, L2, R1, R73, R1, L2, R191, R2, L4, R1, L5, L5, R5, L3, L5, L4, R4, R5, L4, R4, R4, R5, L2, L5, R3, L4, L4, L5, R2, R2, R2, R4, L3, R4, R5, L3, R5, L2, R3, L1, R2, R2, L3, L1, R5, L3, L5, R2, R4, R1, L1, L5, R3, R2, L3, L4, L5, L1, R3, L5, L2, R2, L3, L4, L1, R1, R4, R2, R2, R4, R2, R2, L3, L3, L4, R4, L4, L4, R1, L4, L4, R1, L2, R5, R2, R3, R3, L2, L5, R3, L3, R5, L2, R3, R2, L4, L3, L1, R2, L2, L3, L5, R3, L1, L3, L4, L3 ";
- int ctr = 0;
- for(string::iterator it = str.begin(); it != str.end(); it++) // separates string into direction, + Number
- {
- int dist = 0;
- if (*it == ',' || *it == ' ')
- {
- continue;
- }
- char dir = *it;
- it++;
- if (*(it + 1) != ',' && *(it + 1) != ' ')
- {
- if (*(it + 2) != ',' && *(it + 2) != ' ')
- {
- char a = *it;
- char b = *(it+1);
- char c = *(it+2);
- int d = a - '0';
- int e = b - '0';
- d *= 100;
- e *= 10;
- int f = c - '0';
- dist = d+e+f;
- it+=2;
- }
- else if(*(it + 2) == ',' || *(it + 2) == ' ')
- {
- char a = *it;
- char b = *(it+1);
- int d = a - '0';
- int e = b - '0';
- d *= 10;
- dist = d+e;
- it++;
- }
- }
- else
- {
- dist = *it - '0';
- }
- ctr++;
- moveToPos(dir, dist);
- }
- int totalDist = abs(posX) + abs(posY);
- cout << "total distance from origin " << totalDist << endl;
- int tempX = 0; // P.2
- int tempY = 0;
- for (int i = 0; i < distanceT; i++) //P.2 loops to check for duplicate location
- {
- for (int j = 0; j < distanceT; j++)
- {
- if (poss[i][0] == poss[j][0])
- {
- if (poss[i][1] == poss[j][1] && i!= j)
- {
- tempX = poss[i][0];
- tempY = poss[i][1];
- i = distanceT + 1;
- break;
- }
- }
- }
- }
- int distSame = abs (tempX) + abs(tempY);
- cout << "same " << distSame << endl;
- cout << "distance traveled " << distanceT << endl;
- }
- void moveToPos(char newDir, int dist)
- {
- if (curDir == 'N')
- {
- if (newDir == 'L')
- {
- curDir = 'W';
- incrX = -1;
- incrY = 0;
- }
- if (newDir == 'R')
- {
- curDir = 'E';
- incrX = 1;
- incrY = 0;
- }
- }
- else
- if (curDir == 'S')
- {
- if (newDir == 'L')
- {
- curDir = 'E';
- incrX = 1;
- incrY = 0;
- }
- if (newDir == 'R')
- {
- curDir = 'W';
- incrX = -1;
- incrY = 0;
- }
- }
- else
- if (curDir == 'E')
- {
- if (newDir == 'L')
- {
- curDir = 'N';
- incrX = 0;
- incrY = 1;
- }
- if (newDir == 'R')
- {
- curDir = 'S';
- incrX = 0;
- incrY = -1;
- }
- }
- else
- if (curDir == 'W')
- {
- if (newDir == 'L')
- {
- curDir = 'S';
- incrX = 0;
- incrY = -1;
- }
- if (newDir == 'R')
- {
- curDir = 'N';
- incrX = 0;
- incrY = 1;
- }
- }
- for (int i = 1; i < dist; i++) //P.2 adds all steps to an array. array uses total distance traveled for array size.
- {
- poss[arrctr][0] = posX + incrX * i;
- poss[arrctr][1] = posY + incrY * i;
- arrctr ++;
- }
- posX += incrX * dist;
- posY += incrY * dist;
- distanceT += abs(incrX * dist);
- distanceT += abs(incrX * dist);
- }
Advertisement
Add Comment
Please, Sign In to add comment