Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. uint64_t GCD(uint64_t a, uint64_t b)
  2. {
  3.     if (b == 0)
  4.         return a;
  5.  
  6.     return GCD(b, a % b);
  7. }
  8.  
  9. uint64_t LCM(uint64_t a, uint64_t b)
  10. {
  11.     return a * b / GCD(a, b);
  12. }
  13.  
  14. void Day12_SimulateStep(std::array<int64_t, 4>& pos, std::array<int64_t, 4>& vel)
  15. {
  16.     for (int i = 0; i < 3; i++)
  17.     {
  18.         for (int j = i + 1; j < 4; j++)
  19.         {
  20.             if (pos[i] == pos[j])
  21.                 continue;
  22.  
  23.             if (pos[i] < pos[j])
  24.             {
  25.                 vel[i]++;
  26.                 vel[j]--;
  27.             }
  28.             else
  29.             {
  30.                 vel[i]--;
  31.                 vel[j]++;
  32.             }
  33.         }
  34.     }
  35.  
  36.     for (int i = 0; i < 4; i++)
  37.         pos[i] += vel[i];
  38. }
  39.  
  40. void Day12_1()
  41. {
  42.     std::array<int64_t, 4> posX = { 0, 4, -11, 2 };
  43.     std::array<int64_t, 4> posY = { 6, 4, 1, 19 };
  44.     std::array<int64_t, 4> posZ = { 1, 19, 8, 15 };
  45.  
  46.     std::array<int64_t, 4> velX = {};
  47.     std::array<int64_t, 4> velY = {};
  48.     std::array<int64_t, 4> velZ = {};
  49.  
  50.     for (int i = 0; i < 1000; i++)
  51.         Day12_SimulateStep(posX, velX);
  52.  
  53.     for (int i = 0; i < 1000; i++)
  54.         Day12_SimulateStep(posY, velY);
  55.  
  56.     for (int i = 0; i < 1000; i++)
  57.         Day12_SimulateStep(posZ, velZ);
  58.  
  59.     int64_t sum = 0;
  60.     for (int i = 0; i < 4; i++)
  61.         sum += (abs(posX[i]) + abs(posY[i]) + abs(posZ[i])) * (abs(velX[i]) + abs(velY[i]) + abs(velZ[i]));
  62.  
  63.     std::cout << sum << std::endl;
  64. }
  65.  
  66. void Day12_2()
  67. {
  68.     const std::array<int64_t, 4> initPosX = { 0, 4, -11, 2 };
  69.     const std::array<int64_t, 4> initPosY = { 6, 4, 1, 19 };
  70.     const std::array<int64_t, 4> initPosZ = { 1, 19, 8, 15 };
  71.     const std::array<int64_t, 4> initVel = {};
  72.  
  73.     std::array<int64_t, 4> posX = initPosX;
  74.     std::array<int64_t, 4> posY = initPosY;
  75.     std::array<int64_t, 4> posZ = initPosZ;
  76.  
  77.     std::array<int64_t, 4> velX = {};
  78.     std::array<int64_t, 4> velY = {};
  79.     std::array<int64_t, 4> velZ = {};
  80.  
  81.     int64_t repeatX = -1;
  82.     int64_t repeatY = -1;
  83.     int64_t repeatZ = -1;
  84.  
  85.     for (int64_t i = 1;; i++)
  86.     {
  87.         Day12_SimulateStep(posX, velX);
  88.         if (velX == initVel && posX == initPosX)
  89.         {
  90.             repeatX = i;
  91.             break;
  92.         }
  93.     }
  94.  
  95.     for (int64_t i = 1;; i++)
  96.     {
  97.         Day12_SimulateStep(posY, velY);
  98.         if (velY == initVel && posY == initPosY)
  99.         {
  100.             repeatY = i;
  101.             break;
  102.         }
  103.     }
  104.  
  105.     for (int64_t i = 1;; i++)
  106.     {
  107.         Day12_SimulateStep(posZ, velZ);
  108.         if (velZ == initVel && posZ == initPosZ)
  109.         {
  110.             repeatZ = i;
  111.             break;
  112.         }
  113.     }
  114.  
  115.     std::cout << LCM(LCM(repeatX, repeatY), repeatZ) << std::endl;
  116.     return;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement