Created
December 11, 2024 20:30
-
-
Save maudzekod4000/2f15ea52602743957f878ef9d7043ef8 to your computer and use it in GitHub Desktop.
AOC 2024 Problem 2 Part 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <fstream> | |
| #include <string> | |
| #include <ctype.h> | |
| #include <vector> | |
| #include <algorithm> | |
| #include <unordered_map> | |
| #include <sstream> | |
| using namespace std; | |
| int main() | |
| { | |
| ifstream infile("input.txt"); | |
| if (!infile.is_open()) { | |
| cout << "File prob\n"; | |
| return -1; | |
| } | |
| int safeReportsCount = 0; | |
| string line; | |
| while (infile.good()) { | |
| getline(infile, line); | |
| if (line.empty()) continue; | |
| std::vector<int> report; | |
| std::stringstream ss(line); | |
| while (!ss.eof()) { | |
| int n; | |
| ss >> n; | |
| report.push_back(n); | |
| } | |
| for (int skipIdx = 0; skipIdx < report.size(); skipIdx++) { | |
| std::vector<int> reportWithoutOne = report; | |
| reportWithoutOne.erase(reportWithoutOne.begin() + skipIdx); | |
| std::string newline; | |
| for (int i = 0; i < reportWithoutOne.size(); i++) { | |
| newline += std::to_string(reportWithoutOne[i]); | |
| if (i != reportWithoutOne.size() - 1) { | |
| newline += ' '; | |
| } | |
| } | |
| std::stringstream ss(newline); | |
| bool isSafe = true; | |
| int prev; | |
| int n; | |
| ss >> prev >> n; | |
| int diff = abs(prev - n); | |
| bool incr = n - prev > 0; | |
| prev = n; | |
| if (diff < 1 || diff > 3) { | |
| isSafe = false; | |
| } | |
| while (!ss.eof() && isSafe) { | |
| ss >> n; | |
| bool isIncr = n - prev > 0; | |
| diff = abs(prev - n); | |
| if (isIncr != incr || diff < 1 || diff > 3) { | |
| isSafe = false; | |
| break; | |
| } | |
| prev = n; | |
| } | |
| if (isSafe) { | |
| safeReportsCount++; | |
| break; | |
| } | |
| } | |
| } | |
| infile.close(); | |
| std::cout << safeReportsCount << std::endl; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment