Advertisement
Guest User

Advent2017_12b

a guest
Dec 13th, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1.  
  2. #include <sstream>
  3. #include <iostream>
  4. #include <fstream>
  5.  
  6. const unsigned int MAX_LAYERS = 100;
  7.  
  8. // https://pastebin.com/bNAaTL1e
  9.  
  10. void updateLayer(unsigned int * layer) {
  11.     unsigned int range = layer[0];
  12.     unsigned int position = layer[1];
  13.     unsigned int direction = layer[2];
  14.     if(direction == 0) {
  15.         // down v
  16.         if(position == range - 1) {
  17.             layer[1] = range - 2;
  18.             layer[2] = 1; // change direction to up
  19.         }else {
  20.             layer[1]++;
  21.         }
  22.     }else {
  23.         // up ^
  24.         if(position == 0) {
  25.             layer[1] = 1; // second position
  26.             layer[2] = 0; // go down
  27.         }else {
  28.             layer[1]--;
  29.         }
  30.     }
  31. }
  32.  
  33. int main(int argc, char ** argv) {
  34.     std::ifstream inputFile("input.txt");
  35.    
  36.     unsigned int ** layers = new unsigned int * [MAX_LAYERS];
  37.     unsigned int ** delayState = new unsigned int * [MAX_LAYERS];
  38.     for(unsigned int i = 0; i < MAX_LAYERS; i++) {
  39.         layers[i] = new unsigned int[3];
  40.         layers[i][0] = 0; // range
  41.         layers[i][1] = 0; // current scanner position
  42.         layers[i][2] = 0; // current scanner direction (0 down, 1 up)
  43.        
  44.         delayState[i] = new unsigned int[3];
  45.         delayState[i][0] = 0; // range
  46.         delayState[i][1] = 0; // current scanner position
  47.         delayState[i][2] = 0; // current scanner direction (0 down, 1 up)
  48.     }
  49.    
  50.     if(!inputFile.is_open()) {
  51.         std::cout << "Could not read file." << std::endl;
  52.         return 1;
  53.     }
  54.    
  55.     std::string line;
  56.     while(std::getline(inputFile, line)) {
  57.         std::stringstream ss(line);
  58.        
  59.         unsigned int source = 0;
  60.         unsigned int range = 0;
  61.         ss >> source;
  62.        
  63.         ss.ignore(1, ':');
  64.         ss.ignore(1);
  65.        
  66.         ss >> range;
  67.         layers[source][0] = range;
  68.         delayState[source][0] = range;
  69.     }
  70.    
  71.     unsigned int delay = 0;
  72.    
  73.     while(true) {
  74.         // Reset layers to next delay state:
  75.         for(unsigned int i = 0; i < MAX_LAYERS; i++) {
  76.             layers[i][1] = delayState[i][1]; // current scanner position
  77.             layers[i][2] = delayState[i][2]; // direction
  78.         }
  79.        
  80.         // Look for crashes:
  81.         bool crash = false;
  82.         for(unsigned int depth = 0; depth < MAX_LAYERS; depth++) {
  83.             unsigned int * currentLayer = layers[depth];
  84.            
  85.             if(currentLayer[0] > 0 && currentLayer[1] == 0) {
  86.                 crash = true;
  87.                 break;
  88.             }
  89.            
  90.             // Update all layers from current depth onward:
  91.             for(unsigned int i = depth; i < MAX_LAYERS; i++) {
  92.                 updateLayer(layers[i]);
  93.             }
  94.         }
  95.        
  96.         if(!crash) {
  97.             break;
  98.         }
  99.        
  100.         // Next delay state simulation
  101.         for(unsigned int i = 0; i < MAX_LAYERS; i++) {
  102.             updateLayer(delayState[i]);
  103.         }
  104.        
  105.         delay++;
  106.     }
  107.    
  108.     std::cout << "Delay: " << delay << std::endl;
  109.    
  110.     return 0;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement