Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2023
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <string_view>
  6. #include <set>
  7. size_t getNumStart(std::string_view str, size_t idx)
  8. {
  9.     char ch = str[idx];
  10.     while(std::isdigit(ch))
  11.     {
  12.         idx--;
  13.         ch = str[idx];
  14.     }
  15.     return idx+1;
  16. }
  17. int extractNum(std::string_view str, size_t idx)
  18. {
  19.     std::string num = "";
  20.     char ch = str[idx];
  21.     while(std::isdigit(ch))
  22.     {
  23.         num+=ch;
  24.         idx++;
  25.         ch = str[idx];
  26.     }
  27.     return std::stoi(num);
  28. }
  29.  
  30. std::set<size_t> countedNumbers;
  31. int countPosition(std::string_view str, size_t idx)
  32. {
  33.  
  34.     if(std::isdigit(str[idx]))
  35.     {
  36.         size_t sIdx = getNumStart(str, idx);
  37.         if(countedNumbers.find(sIdx)==countedNumbers.end())
  38.         {
  39.             countedNumbers.insert(sIdx);
  40.             return extractNum(str, sIdx);
  41.         }
  42.     }
  43.     return 0;
  44. }
  45.  
  46. int processPositionP1(std::string_view str, size_t idx, const std::vector<int> &offsets)
  47. {
  48.  
  49.     int sum = 0;
  50.     char ch = str[idx];
  51.     if(ch != '.' && !std::isdigit(ch))
  52.     {
  53.        
  54.         for(int offset: offsets)
  55.         {
  56.             sum += countPosition(str, idx+offset);
  57.         }
  58.     }
  59.     return sum;
  60. }
  61. int processPositionP2(std::string_view str, size_t idx, const std::vector<int> &offsets)
  62. {
  63.  
  64.     int sum = 0;
  65.     char ch = str[idx];
  66.     if(ch == '*')
  67.     {
  68.         int counted = 0;
  69.         for(int offset: offsets)
  70.         {
  71.             int pos = countPosition(str, idx+offset);
  72.             if(pos!=0)
  73.             {
  74.                 sum = (sum==0) ? pos : pos*sum;
  75.                 counted++;
  76.             }
  77.             if(counted>=2)break;
  78.         }
  79.         if(counted==2)
  80.         {
  81.            
  82.             return sum;
  83.         }
  84.     }
  85.     return 0;
  86. }
  87. int main()
  88. {
  89.     auto file = std::fstream("./input", std::ios_base::in);
  90.     std::string text;
  91.     int lineLen = 0;
  92.     long int sump1 = 0;
  93.     long int sump2 = 0;
  94.  
  95.     while(file)
  96.     {
  97.         std::string s;
  98.         std::getline(file,s);
  99.         lineLen = (lineLen) ? lineLen : s.size()+1;
  100.         text+=s+'.';
  101.     }
  102.     file.close();
  103.    
  104.     std::vector<int> offsets = {1,-1,lineLen,-lineLen,lineLen+1, lineLen-1, -lineLen+1, -lineLen-1};
  105.     for(size_t i = 0; i < text.size(); i++)
  106.     {
  107.         sump1 += processPositionP1(text, i, offsets);
  108.     }
  109.     countedNumbers.clear();
  110.     for(size_t i = 0; i < text.size(); i++)
  111.     {
  112.         sump2 += processPositionP2(text, i, offsets);
  113.     }
  114.     std::cerr << "p1: " << sump1 << " p2: " << sump2 << std::endl;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement