Advertisement
Guest User

--- Day 8: Handheld Halting ---

a guest
Dec 8th, 2020
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #include <string>
  2. #include <vector>
  3. #include <sstream>
  4. #include <iostream>
  5. #include <iterator>
  6.  
  7. void RestoreProgram(std::vector<std::string>& assembly)
  8. {
  9.     for (unsigned int i = 0; i < assembly.size(); i++)
  10.     {
  11.         std::string& command = assembly[i];
  12.         if (command[command.size() - 1] == '!')
  13.         {
  14.             command[command.size() - 1] = '\n';
  15.         }
  16.     }
  17. }
  18.  
  19. bool RunProgram(std::vector<std::string>& assembly)
  20. {
  21.     int acc = 0;
  22.     int pos = 0;
  23.    
  24.     while (true)
  25.     {
  26.         if (pos >= assembly.size()) //Assume program ended successfully
  27.         {
  28.             std::cout << "Accumulator returned: " << acc << std::endl;
  29.             RestoreProgram(assembly);
  30.             return true;
  31.         }
  32.        
  33.         std::string& command = assembly[pos];
  34.        
  35.         std::istringstream iss(command);
  36.         std::vector<std::string> results(std::istream_iterator<std::string>{iss},
  37.             std::istream_iterator<std::string>());
  38.        
  39.         if (command[command.size() - 1] == '!')
  40.         {
  41.             std::cout << "Infinite Loop Error! Last Pos: " << pos << std::endl;
  42.             RestoreProgram(assembly);
  43.             return false;
  44.         }
  45.         else if (results[0] == "acc")
  46.         {
  47.             acc += atoi(results[1].c_str());
  48.             pos++;
  49.         }
  50.         else if (results[0] == "jmp")
  51.         {
  52.             pos += atoi(results[1].c_str());
  53.         }
  54.         else //Assume nop
  55.         {
  56.             pos++;
  57.         }
  58.        
  59.         //std::cout << "Command: " << results[0] << " " << results[1] << " Pos: " << pos << " Acc " << acc << std::endl;
  60.         command[command.size() - 1] = '!';
  61.     }
  62. }
  63.  
  64. int main()
  65. {
  66.     std::vector<std::string> instructions;
  67.    
  68.     std::string text;
  69.     while (getline(std::cin, text))
  70.         instructions.push_back(text);
  71.        
  72.     std::cout << "Number of Instructions: " << instructions.size() << std::endl;
  73.    
  74.     for (unsigned int i = 0; i < instructions.size(); i++)
  75.     {
  76.         if (instructions[i][0] == 'n')
  77.         {
  78.             instructions[i][0] = 'j';
  79.             instructions[i][1] = 'm';
  80.             if (!RunProgram(instructions))
  81.             {
  82.                 instructions[i][0] = 'n';
  83.                 instructions[i][1] = 'o';
  84.             }
  85.             else
  86.             {
  87.                 std::cout << "Corrupted NOP found at line: " << i << std::endl;
  88.                 return 0;
  89.             }
  90.         }
  91.         else if (instructions[i][0] == 'j')
  92.         {
  93.             instructions[i][0] = 'n';
  94.             instructions[i][1] = 'o';
  95.             if (!RunProgram(instructions))
  96.             {
  97.                 instructions[i][0] = 'j';
  98.                 instructions[i][1] = 'm';
  99.             }
  100.             else
  101.             {
  102.                 std::cout << "Corrupted JMP found at line: " << i << std::endl;
  103.                 return 0;
  104.             }
  105.         }
  106.     }
  107.    
  108.     std::cout << "No fix found! :(" << std::endl;
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement