IoIiderp

Oversized Pancake Flipper

Sep 10th, 2017
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. // Solution for:
  2. // https://code.google.com/codejam/contest/3264486/dashboard
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. #include <stdlib.h>
  7. #include <sstream>
  8.  
  9. const std::string INPUT_FILE_PATH  = "D:\\Downloads\\input.txt";
  10. const std::string OUTPUT_FILE_PATH = "D:\\Downloads\\output.txt";
  11.  
  12.  
  13. int main()
  14. {
  15.     unsigned short LineNumber = -1;
  16.  
  17.     std::ofstream OutputFile;
  18.     std::ifstream InputFile;
  19.  
  20.     OutputFile.open( OUTPUT_FILE_PATH.c_str(), std::ios::trunc );
  21.     InputFile.open ( INPUT_FILE_PATH.c_str() );
  22.  
  23.  
  24.     std::string line;
  25.     while( std::getline(InputFile, line) )
  26.     {
  27.         if( ++LineNumber == 0 ) continue; // increase the line number but skip the first one (due to it being the number of lines)
  28.  
  29.  
  30.         std::cout << "Now doing line: " << LineNumber+1 << std::endl;
  31.  
  32.         std::string Pancakes = "";
  33.         int SizeFlipper = 0;
  34.  
  35.  
  36.         // Generate Pancakes & SizeFlipper variables
  37.         std::string tmp_sizeflipper = "";
  38.  
  39.         for(unsigned int i=0; i<line.length(); ++i)
  40.         {
  41.             if(line[i]=='-' || line[i]=='+')
  42.                 Pancakes.push_back(line[i]);
  43.  
  44.             if(line[i]>='0' && line[i]<='9')
  45.                 tmp_sizeflipper.push_back(line[i]);
  46.         }
  47.  
  48.         SizeFlipper = atoi( tmp_sizeflipper.c_str() );
  49.  
  50.  
  51.         // Start flipping!
  52.         int Flips = 0;
  53.  
  54.         for(unsigned int i=0; i<Pancakes.length()-SizeFlipper+1; ++i)
  55.         {
  56.             if( Pancakes[i]=='-' )
  57.             {
  58.                 for(unsigned int t=0; t<SizeFlipper; ++t)
  59.                 {
  60.                     Pancakes[i+t] = (Pancakes[i+t]=='-'?'+':'-');
  61.                 }
  62.  
  63.                 Flips++;
  64.             }
  65.         }
  66.  
  67.  
  68.         // Okay then let's generate the results!
  69.         std::stringstream stream;
  70.         stream << Flips;
  71.         std::string Flips_Result = stream.str();
  72.  
  73.         for(unsigned int i=0; i<Pancakes.length(); ++i)
  74.         {
  75.             if(Pancakes[i]=='-')
  76.             {
  77.                 Flips_Result = "IMPOSSIBLE";
  78.                 break;
  79.             }
  80.         }
  81.  
  82.  
  83.         OutputFile << "Case #" << LineNumber << ": " << Flips_Result << std::endl;
  84.  
  85.     }
  86.  
  87.  
  88.     OutputFile.close();
  89.     InputFile.close();
  90.  
  91.  
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment