Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4.  
  5. class IntCode
  6. {
  7. private:
  8.     std::vector<int> memory{};
  9.  
  10.     enum Opcode {
  11.         ADD = 1,
  12.         MUL = 2,
  13.         INP = 3,
  14.         OUT = 4,
  15.         HALT = 99
  16.     };
  17.  
  18.     class ModesList
  19.     {
  20.     private:
  21.         std::vector<bool> modes{};
  22.  
  23.     public:
  24.         void push_back(bool val) { modes.push_back(val); }
  25.         bool operator[](size_t idx)
  26.         {
  27.             if (idx >= modes.size())
  28.                 return 0;
  29.             return modes[idx];
  30.         }
  31.     };
  32.  
  33.     struct ModedOp
  34.     {
  35.         int opcode;
  36.         ModesList modes;
  37.     };
  38.  
  39.     ModedOp toModedOp(int val)
  40.     {
  41.         int opcode = val % 100;
  42.         ModesList modes{};
  43.  
  44.         val -= opcode;
  45.         for (int i = 1000; val != 0; i *= 10)
  46.         {
  47.             int nMode = val % i;
  48.             modes.push_back(nMode);
  49.             val -= nMode;
  50.         }
  51.  
  52.         return { opcode, modes };
  53.     }
  54.  
  55.     int& addr(int loc) { return memory[memory[loc]]; }
  56.     int read(int val, bool mode) { return (mode) ? memory[val] : addr(val); }
  57.  
  58. public:
  59.     IntCode() {}
  60.     IntCode(std::vector<int> intCode) : memory(intCode) {}
  61.     int& operator[](int loc) { return memory[loc]; }
  62.  
  63.     std::vector<int> run(const std::vector<int>& input)
  64.     {
  65.         int inputPtr = 0;
  66.         std::vector<int> out{};
  67.  
  68.         for (int ptr = 0; memory[ptr] != HALT;)
  69.         {
  70.             ModedOp mop = toModedOp(memory[ptr]);
  71.             switch (mop.opcode)
  72.             {
  73.             case ADD:
  74.                 addr(ptr + 3) = read(ptr + 1, mop.modes[0]) + read(ptr + 2, mop.modes[1]);
  75.                 ptr += 4;
  76.                 break;
  77.             case MUL:
  78.                 addr(ptr + 3) = read(ptr + 1, mop.modes[0]) * read(ptr + 2, mop.modes[1]);
  79.                 ptr += 4;
  80.                 break;
  81.             case INP:
  82.                 addr(ptr + 1) = input[inputPtr++];
  83.                 ptr += 2;
  84.                 break;
  85.             case OUT:
  86.                 out.push_back(addr(ptr + 1));
  87.                 ptr += 2;
  88.                 break;
  89.             }
  90.         }
  91.  
  92.         return out;
  93.     }
  94.  
  95.     void readFrom(const std::string& fileName)
  96.     {
  97.         std::ifstream inf(fileName);
  98.         int temp{};
  99.         while (inf >> temp)
  100.         {
  101.             memory.push_back(temp);
  102.             inf.ignore(2, ',');
  103.         }
  104.     }
  105. };
  106.  
  107. void part1()
  108. {
  109.     IntCode code;
  110.     code.readFrom("input.txt");
  111.     auto out = code.run({1});
  112.  
  113.     for (auto val : out)
  114.         std::cout << val << " ";
  115.     // 3 0 0 0 0 0 0 0 0 9219874
  116. }
  117.  
  118. void part2()
  119. {
  120.  
  121. }
  122.  
  123. int main()
  124. {
  125.     part1();
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement