SHARE
TWEET

Untitled

a guest Dec 7th, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "../../AOCHeaders/stdafx.h"
  2.  
  3.  
  4. class Amplifiers
  5. {
  6. public:
  7.     Amplifiers(std::vector<int> integers, int currPosition = 0, int firstInput = 0, int secondInput = 0) :
  8.         integers(integers), currPosition(currPosition), firstInput(firstInput), secondInput(secondInput) {}
  9.  
  10. public:
  11.     std::vector<int> integers;
  12.     int currPosition;
  13.     int firstInput, secondInput;
  14. };
  15.  
  16.  
  17. void readInput(std::fstream& in, std::vector<int>& integers)
  18. {
  19.     int number = 0;
  20.     char aux{};
  21.  
  22.     while (in >> number)
  23.     {
  24.         integers.push_back(number);
  25.         in >> aux;
  26.     }
  27. }
  28.  
  29.  
  30. void intCodeProgram(std::fstream& out, std::vector<int> integers, std::vector<Amplifiers> amplifiers, int& output)
  31. {
  32.     int currAmplifier = 0;
  33.     int currPos = 0;
  34.     int posMode1 = 0;
  35.     int posMode2 = 0;
  36.     int posMode3 = 0;
  37.  
  38.     while (integers[currPos] != 99)
  39.     {
  40.  
  41.         posMode1 = (integers[currPos] / 100 % 10 == 1) ? currPos + 1 : (currPos + 1 < integers.size()) ? integers[currPos + 1] : 0;
  42.         posMode2 = (integers[currPos] / 1000 % 10 == 1) ? currPos + 2 : (currPos + 2 < integers.size()) ? integers[currPos + 2] : 0;
  43.         posMode3 = (integers[currPos] / 10000 % 10 == 1) ? currPos + 3 : (currPos + 3 < integers.size()) ? integers[currPos + 3] : 0;
  44.  
  45.         switch (integers[currPos] % 100)
  46.         {
  47.         case 1:
  48.             integers[posMode3] = integers[posMode2] + integers[posMode1];
  49.             currPos = currPos + 4;
  50.             break;
  51.  
  52.         case 2:
  53.             integers[posMode3] = integers[posMode2] * integers[posMode1];
  54.             currPos = currPos + 4;
  55.             break;
  56.  
  57.         case 3:
  58.             integers[integers[currPos + 1]] = amplifiers[currAmplifier].firstInput;
  59.             amplifiers[currAmplifier].firstInput = amplifiers[currAmplifier].secondInput;
  60.             currPos = currPos + 2;
  61.             break;
  62.  
  63.         case 4:
  64.             output = integers[integers[currPos + 1]];
  65.             currPos = currPos + 2;
  66.  
  67.             amplifiers[currAmplifier].integers = integers;
  68.             amplifiers[currAmplifier].currPosition = currPos;
  69.  
  70.             currAmplifier = (currAmplifier + 1) % 5;
  71.  
  72.             integers = amplifiers[currAmplifier].integers;
  73.             currPos = amplifiers[currAmplifier].currPosition;
  74.             if (amplifiers[currAmplifier].firstInput == amplifiers[currAmplifier].secondInput)
  75.             {
  76.                 amplifiers[currAmplifier].firstInput = output;
  77.             }
  78.             amplifiers[currAmplifier].secondInput = output;
  79.             break;
  80.  
  81.         case 5:
  82.             currPos = integers[posMode1] != 0 ? integers[posMode2] : currPos + 3;
  83.             break;
  84.  
  85.         case 6:
  86.             currPos = integers[posMode1] == 0 ? integers[posMode2] : currPos + 3;
  87.             break;
  88.  
  89.         case 7:
  90.             integers[posMode3] = integers[posMode1] < integers[posMode2];
  91.             currPos = currPos + 4;
  92.             break;
  93.  
  94.         case 8:
  95.             integers[posMode3] = integers[posMode1] == integers[posMode2];
  96.             currPos = currPos + 4;
  97.             break;
  98.         }
  99.     }
  100. }
  101.  
  102.  
  103. int main()
  104. {
  105.     std::fstream in("input.in", std::fstream::in);
  106.     std::fstream out("output.out", std::fstream::out);
  107.     std::vector<int> integers;
  108.  
  109.     readInput(in, integers);
  110.  
  111.     int output = 0;
  112.     int max = 0;
  113.  
  114.     std::vector<Amplifiers> amplifiers(5, integers);
  115.     std::vector<int> perm{ 5,6,7,8,9 };
  116.  
  117.     do {
  118.         output = 0;
  119.         for (int it = 0; it < perm.size(); it++)
  120.         {
  121.             amplifiers[it].firstInput = perm[it];
  122.         }
  123.  
  124.         intCodeProgram(out, integers, amplifiers, output);
  125.  
  126.         if (max < output)
  127.         {
  128.             max = output;
  129.         }
  130.  
  131.     } while (std::next_permutation(perm.begin(), perm.end()));
  132.  
  133.     out << max;
  134.  
  135.     in.close();
  136.     out.close();
  137. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top