Advertisement
venyanwarrior

AoC 2024 - Day 2 Part 2

Dec 26th, 2024
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | Source Code | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <sstream>
  6.  
  7. bool isLevelValid(std::vector<int> level);
  8. bool canBeSafe(std::vector<int> level);
  9. bool isNotInRange(int num1, int num2);
  10. std::vector<int> stringNumsToVector(std::string);
  11.  
  12. int main() {
  13.     std::ifstream infile;
  14.  
  15.     infile.open("../input.txt");
  16.     if (!infile.is_open()) {
  17.         std::cout << "Input file opening failed." << std::endl;
  18.         exit(1);
  19.     }
  20.  
  21.     std::vector<std::string> fileData;
  22.  
  23.     for (u_int i = 0; !infile.eof(); i++) {
  24.         fileData.push_back("");
  25.         getline(infile, fileData.at(i));
  26.     }
  27.  
  28.     int numSafeReports = 0;
  29.     for (std::string level : fileData) {
  30.         if (isLevelValid(stringNumsToVector(level)) || canBeSafe(stringNumsToVector(level))) {
  31.             std::cout << "Safe" << std::endl;
  32.             numSafeReports++;
  33.         } else {
  34.             std::cout << "Unsafe" << std::endl;
  35.         }
  36.     }
  37.  
  38.     std::cout << "\nSafe reports: " << numSafeReports << std::endl;
  39. }
  40.  
  41. bool isLevelValid(std::vector<int> level) {
  42.     bool ascendingOrDescending = false;
  43.     //*Check if the level is in ascending order
  44.     if (std::is_sorted(level.begin(), level.end())) {
  45.         ascendingOrDescending = true;
  46.     }
  47.  
  48.     //*Check if the level is in descending order
  49.     std::reverse(level.begin(), level.end());
  50.     if (std::is_sorted(level.begin(), level.end())) {
  51.         ascendingOrDescending = true;
  52.     }
  53.     std::reverse(level.begin(), level.end()); //Reverse back to normal
  54.  
  55.     if (ascendingOrDescending == false) {
  56.         return false;
  57.     }
  58.  
  59.     //*Check that no two values are the same
  60.     if (std::adjacent_find(level.begin(), level.end()) != level.end()) {
  61.         return false;
  62.     }
  63.  
  64.     //*Check that adjacent values differ by 1-3
  65.     if (std::adjacent_find(level.begin(), level.end(), isNotInRange) != level.end()) {
  66.         return false;
  67.     }
  68.  
  69.     //* If all tests pass, the level is safe
  70.     return true;
  71. }
  72.  
  73. bool canBeSafe(std::vector<int> level) {
  74.     std::vector<int> tempLevel(level.size()); //*Init so tempIt pos can be set before loop
  75.     std::vector<int>::iterator tempIt = tempLevel.begin();
  76.     for (std::vector<int>::iterator levelIt = level.begin(); levelIt != level.end(); levelIt++, tempIt++) {
  77.         tempLevel = level;
  78.         tempLevel.erase(tempIt);
  79.         if (isLevelValid(tempLevel)) {
  80.             return true;
  81.         }
  82.     }
  83.     return false;
  84. }
  85.  
  86. bool isNotInRange(int num1, int num2) {
  87.     if ((abs(num1 - num2) < 1) || (abs(num1 - num2) > 3)) {
  88.         return true;
  89.     }
  90.     return false;
  91. }
  92.  
  93. std::vector<int> stringNumsToVector(std::string str) {
  94.     std::stringstream ss(str);
  95.     std::vector<int> stringNums;
  96.     std::string currentNum;
  97.  
  98.     while (std::getline(ss, currentNum, ' ')) {
  99.         stringNums.push_back(stoi(currentNum));
  100.     }
  101.     return stringNums;
  102. }
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement