Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <fstream>
  5. #include <bitset>
  6. #include <map>
  7. /* Our direction always starts north */
  8. char direction = 0b1;
  9.  
  10. /* Bit flag for our direction, this enables bit shifting */
  11. enum Dir : int
  12. {
  13. North = 0b1,
  14. East = 0b10,
  15. South = 0b100,
  16. West = 0b1000
  17. };
  18. struct Vector
  19. {
  20. int x;
  21. int y;
  22. };
  23. bool operator<(const Vector& lhs, const Vector& rhs)
  24. {
  25. if (rhs.x < lhs.x)
  26. {
  27. return true;
  28. }
  29. else if (rhs.x > lhs.x)
  30. {
  31. return false;
  32. }
  33. else
  34. {
  35. return lhs.y < rhs.y;
  36. }
  37. return false;
  38. }
  39. char circ_left(char n)
  40. {
  41. n = (n << 1) | (n >> (3));
  42. /* Only return last 4 bits */
  43. return n & 0xF;
  44. }
  45. char circ_right(char n)
  46. {
  47. n = (n>>1) | (n << (3));
  48. return n & 0xF;
  49. }
  50.  
  51. int main(const int argc, const char** argvs)
  52. {
  53. std::ifstream file;
  54. file.open("input.txt");
  55.  
  56. // Read file
  57. std::vector<std::string> commands;
  58. std::string line;
  59. while (std::getline(file, line))
  60. {
  61. /* For each line check if we still have a delimiter -> read*/
  62. while (line.find(',') != std::string::npos)
  63. {
  64. int id = line.find(',');
  65. commands.push_back(line.substr(0, id));
  66.  
  67. line = line.substr(id + 2);
  68. }
  69.  
  70. if(!line.empty())
  71. commands.push_back(line);
  72. }
  73.  
  74. // #suckacock
  75. /* Process each command and advance our position */
  76. Vector position{ 0,0 };
  77. Vector *firstDoublePos = nullptr;
  78. std::map<Vector,int> visited;
  79. for (auto cmd = commands.begin(); cmd != commands.end(); ++cmd)
  80. {
  81. switch (cmd->front()) {
  82. case 'L':
  83. direction = circ_right(direction);
  84. break;
  85. case 'R':
  86. direction = circ_left(direction);
  87. break;
  88. }
  89.  
  90. /* Advance towards that direction */
  91. std::string c = cmd->substr(1);
  92. int blocks = stoi(c);
  93.  
  94. /* CHECK ALL DEM POS */
  95. for (int i = 0; i < blocks; ++i)
  96. {
  97. switch (direction)
  98. {
  99. case North:
  100. position.y += 1;
  101. break;
  102. case East:
  103. position.x += 1;
  104. break;
  105. case West:
  106. position.x -= 1;
  107. break;
  108. case South:
  109. position.y -= 1;
  110. break;
  111. }
  112.  
  113. /* Check my map lellelelelelel */
  114. auto it = visited.find(position);
  115. if (it != visited.end()) {
  116. /* LAZY, use BREAKPOInt FGT*/
  117. std::cout <<"Twice visited is at " << position.x << ", " << position.y << "(" << abs(position.x)+ abs(position.y) << ") " << std::endl;
  118. }
  119.  
  120. visited[position]++;
  121. }
  122.  
  123. }
  124.  
  125. /* MEER LEL */
  126. std::cout << "Final position is " << position.x << "," << position.y << std::endl;
  127. std::cout << abs(position.x) + abs(position.y) << std::endl;
  128. std::cin.get();
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement