Created
December 11, 2024 19:40
-
-
Save maudzekod4000/1b7199382e153080259f9d555a7d4a8d to your computer and use it in GitHub Desktop.
AOC 2024 - Problem 2 Part 1
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::stringstream ss(line); | |
| 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++; | |
| } | |
| } | |
| 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