Advertisement
goeyj

Untitled

Dec 12th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include "../aoc.h"
  2.  
  3. class Ship {
  4. private:
  5.   int x = 0;
  6.   int y = 0;
  7.   int offsetX = 10;
  8.   int offsetY = 1;
  9.   int direction = 90;
  10.   std::map<int, char> directions { {0, 'N'}, {90, 'E'}, {180, 'S'}, {270, 'W'} };
  11.  
  12. public:
  13.   int getX() { return x; }
  14.   int getY() { return y; }
  15.  
  16.   void updatePosition(std::string &instruction, int mode) {
  17.     char action = instruction.at(0);
  18.     int units = std::stoi(instruction.substr(1));
  19.  
  20.     if (mode == 1) {
  21.       switch(action) {
  22.         case 'N': y += units; break;
  23.         case 'E': x += units; break;
  24.         case 'S': y -= units; break;
  25.         case 'W': x -= units; break;
  26.         case 'L': direction = (direction + (360 - units)) % 360; break;
  27.         case 'R': direction = (direction + units) % 360; break;
  28.         case 'F':
  29.           std::string newInstruction;
  30.           newInstruction += directions.at(direction) + instruction.substr(1);
  31.           updatePosition(newInstruction, 1);
  32.           break;
  33.       }
  34.     } else if (mode == 2) {
  35.       if (action == 'F') {
  36.         x += abs(offsetX * units) * (offsetX < 0 ? -1 : 1);
  37.         y += abs(offsetY * units) * (offsetY < 0 ? -1 : 1);
  38.       } else {
  39.         updateWaypoint(action, units);
  40.       }
  41.     }
  42.   }
  43.  
  44.   void updateWaypoint(char action, int units) {
  45.     switch(action) {
  46.       case 'N': offsetY += units; break;
  47.       case 'E': offsetX += units; break;
  48.       case 'S': offsetY -= units; break;
  49.       case 'W': offsetX -= units; break;
  50.       case 'L': rotateWaypoint(units / 90, 'L'); break;
  51.       case 'R': rotateWaypoint(units / 90, 'R'); break;
  52.     }
  53.   }
  54.  
  55.   void rotateWaypoint(int times, char mode) {
  56.     for (int i = 0; i < times; ++i) {
  57.       int tempY;
  58.  
  59.       if (mode == 'L') {
  60.         tempY = offsetX;
  61.         offsetX = -offsetY;
  62.         offsetY = tempY;
  63.       }
  64.  
  65.       if (mode == 'R') {
  66.         tempY = -offsetX;
  67.         offsetX = offsetY;
  68.         offsetY = tempY;
  69.       }
  70.     }
  71.   }
  72. };
  73.  
  74. int main() {
  75.   Ship ship1;
  76.   Ship ship2;
  77.  
  78.   std::ifstream input("input.txt", std::ios::in);
  79.   if (input.is_open()) {
  80.     std::string instruction;
  81.     enum modes { part1 = 1, part2 = 2 };
  82.  
  83.     while(getline(input, instruction)) {
  84.       ship1.updatePosition(instruction, part1);
  85.       ship2.updatePosition(instruction, part2);
  86.     }
  87.  
  88.     input.close();
  89.   }
  90.  
  91.   std::cout << "Part 1: " << abs(ship1.getX()) + abs(ship1.getY()) << std::endl;
  92.   std::cout << "Part 2: " << abs(ship2.getX()) + abs(ship2.getY()) << std::endl;
  93.  
  94.   return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement