wheelsmanx

John DAY1_ Part 1+2

Feb 2nd, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // adventofcode
  4. //
  5. // Created by Jonathan Hirsch on 1/26/17.
  6. // Copyright © 2017 Jonathan Hirsch. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <sstream>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13.  
  14. using namespace std;
  15.  
  16. int poss [948][2]; // 948 = total distance traveled. Modify as needed. Code will print out total distance travelled.
  17. int arrctr = 0;
  18. int distanceT = 0;
  19. int incrX;
  20. int incrY;
  21. int posX;
  22. int posY;
  23. char curDir;
  24. void moveToPos(char curDir, int dist);
  25.  
  26.  
  27. int main()
  28. {
  29. incrX = 0;
  30. incrY = 0;
  31. posX = 0;
  32. posY = 0;
  33.  
  34. curDir = 'N';
  35.  
  36.  
  37. 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 ";
  38.  
  39. int ctr = 0;
  40. for(string::iterator it = str.begin(); it != str.end(); it++) // separates string into direction, + Number
  41. {
  42. int dist = 0;
  43. if (*it == ',' || *it == ' ')
  44. {
  45. continue;
  46. }
  47. char dir = *it;
  48.  
  49. it++;
  50.  
  51. if (*(it + 1) != ',' && *(it + 1) != ' ')
  52. {
  53. if (*(it + 2) != ',' && *(it + 2) != ' ')
  54. {
  55. char a = *it;
  56. char b = *(it+1);
  57. char c = *(it+2);
  58.  
  59.  
  60. int d = a - '0';
  61. int e = b - '0';
  62.  
  63. d *= 100;
  64. e *= 10;
  65.  
  66. int f = c - '0';
  67.  
  68.  
  69. dist = d+e+f;
  70. it+=2;
  71. }
  72. else if(*(it + 2) == ',' || *(it + 2) == ' ')
  73. {
  74. char a = *it;
  75. char b = *(it+1);
  76.  
  77. int d = a - '0';
  78.  
  79. int e = b - '0';
  80.  
  81. d *= 10;
  82.  
  83. dist = d+e;
  84. it++;
  85. }
  86. }
  87. else
  88. {
  89. dist = *it - '0';
  90. }
  91. ctr++;
  92. moveToPos(dir, dist);
  93.  
  94. }
  95.  
  96.  
  97. int totalDist = abs(posX) + abs(posY);
  98.  
  99. cout << "total distance from origin " << totalDist << endl;
  100.  
  101.  
  102. int tempX = 0; // P.2
  103. int tempY = 0;
  104.  
  105. for (int i = 0; i < distanceT; i++) //P.2 loops to check for duplicate location
  106. {
  107. for (int j = 0; j < distanceT; j++)
  108. {
  109. if (poss[i][0] == poss[j][0])
  110. {
  111. if (poss[i][1] == poss[j][1] && i!= j)
  112. {
  113. tempX = poss[i][0];
  114. tempY = poss[i][1];
  115.  
  116. i = distanceT + 1;
  117. break;
  118. }
  119. }
  120. }
  121. }
  122.  
  123. int distSame = abs (tempX) + abs(tempY);
  124.  
  125. cout << "same " << distSame << endl;
  126.  
  127. cout << "distance traveled " << distanceT << endl;
  128.  
  129. }
  130.  
  131.  
  132.  
  133.  
  134. void moveToPos(char newDir, int dist)
  135. {
  136. if (curDir == 'N')
  137. {
  138. if (newDir == 'L')
  139. {
  140. curDir = 'W';
  141. incrX = -1;
  142. incrY = 0;
  143. }
  144.  
  145. if (newDir == 'R')
  146. {
  147. curDir = 'E';
  148. incrX = 1;
  149. incrY = 0;
  150. }
  151. }
  152. else
  153. if (curDir == 'S')
  154. {
  155. if (newDir == 'L')
  156. {
  157. curDir = 'E';
  158. incrX = 1;
  159. incrY = 0;
  160. }
  161.  
  162. if (newDir == 'R')
  163. {
  164. curDir = 'W';
  165. incrX = -1;
  166. incrY = 0;
  167. }
  168. }
  169. else
  170. if (curDir == 'E')
  171. {
  172. if (newDir == 'L')
  173. {
  174. curDir = 'N';
  175. incrX = 0;
  176. incrY = 1;
  177. }
  178.  
  179. if (newDir == 'R')
  180. {
  181. curDir = 'S';
  182. incrX = 0;
  183. incrY = -1;
  184. }
  185. }
  186. else
  187. if (curDir == 'W')
  188. {
  189. if (newDir == 'L')
  190. {
  191. curDir = 'S';
  192. incrX = 0;
  193. incrY = -1;
  194. }
  195.  
  196. if (newDir == 'R')
  197. {
  198. curDir = 'N';
  199. incrX = 0;
  200. incrY = 1;
  201. }
  202. }
  203.  
  204. for (int i = 1; i < dist; i++) //P.2 adds all steps to an array. array uses total distance traveled for array size.
  205. {
  206. poss[arrctr][0] = posX + incrX * i;
  207. poss[arrctr][1] = posY + incrY * i;
  208.  
  209. arrctr ++;
  210. }
  211.  
  212.  
  213. posX += incrX * dist;
  214. posY += incrY * dist;
  215.  
  216. distanceT += abs(incrX * dist);
  217. distanceT += abs(incrX * dist);
  218.  
  219. }
Advertisement
Add Comment
Please, Sign In to add comment