diff --git a/days/day02.cpp b/days/day02.cpp index ccf3712..09ab9f3 100644 --- a/days/day02.cpp +++ b/days/day02.cpp @@ -71,44 +71,10 @@ void day02::Calculate() return; } - int safeReports = 0; - bool increasing, decreasing, safe; - - //std::string line; for (std::string line; std::getline(input, line, '\n');) { - std::istringstream iss(line); - std::vector numbers(std::istream_iterator{iss}, std::istream_iterator()); - int previousNumber = numbers.at(0); - increasing = false; - decreasing = false; - safe = true; - for (int i = 1; i < numbers.size(); i++) - { - int currentNumber = numbers.at(i); - - if (i == 1) - { - if (currentNumber > previousNumber) - { - increasing = true; - } - if (currentNumber < previousNumber) - { - decreasing = true; - } - } - - if (IsUnsafe(currentNumber, previousNumber, increasing, decreasing) == 1) - { - safe = false; - break; - } - - previousNumber = currentNumber; - } - if (safe) + if (IsSafe(line)) { safeReports++; } @@ -116,14 +82,65 @@ void day02::Calculate() std::cout << safeReports << '\n'; } -int day02::IsUnsafe(const int current, const int previous, const bool increasing, const bool decreasing) +bool day02::IsSafe(const std::string &line) +{ + std::istringstream iss(line); + std::vector numbers(std::istream_iterator{iss}, std::istream_iterator()); + int previousNumber = numbers.at(numbers.size() - 1); + bool increasing = false; + bool decreasing = false; + bool safe = true; + for (int i = numbers.size() - 2; i >= 0; i--) + { + const int currentNumber = numbers.at(i); + + if (i == numbers.size() - 2) + { + if (previousNumber < currentNumber) + { + increasing = true; + } + if (previousNumber > currentNumber) + { + decreasing = true; + } + } + + if (SafetyChecks(currentNumber, previousNumber, increasing, decreasing) == 1) + { + // numbers.erase(numbers.begin() + i); + // if (i + 1 < numbers.size()) + // { + // previousNumber = numbers.at(i + 1); + // } else + // { + // printf("wtf do i do here?"); + // } + // //currentNumber = numbers.at(i); + // if (SafetyChecks(currentNumber, previousNumber, increasing, decreasing) == 1) + // { + safe = false; + return false; + // } + } + + previousNumber = currentNumber; + } + if (safe) + { + return true; + } + return false; +} + +int day02::SafetyChecks(const int current, const int previous, const bool increasing, const bool decreasing) { const int diff = abs(current - previous); - if (current < previous && increasing) + if (previous > current && increasing) { return 1; } - if (current > previous && decreasing) + if (previous < current && decreasing) { return 1; } diff --git a/days/day02.h b/days/day02.h index 1619e74..a44b34e 100644 --- a/days/day02.h +++ b/days/day02.h @@ -4,6 +4,7 @@ #ifndef DAY02_H #define DAY02_H +#include class day02 @@ -11,7 +12,9 @@ class day02 public: static void Calculate(); - static int IsUnsafe(int current, int previous, bool increasing, bool decreasing); + static bool IsSafe(const std::string &line); + + static int SafetyChecks(int current, int previous, bool increasing, bool decreasing); }; #endif //DAY02_H diff --git a/input/day02short.txt b/input/day02short.txt index 12f4045..c448359 100644 --- a/input/day02short.txt +++ b/input/day02short.txt @@ -3,4 +3,6 @@ 7 6 4 2 1 1 2 7 8 9 9 7 6 2 1 -1 3 6 7 9 \ No newline at end of file +1 3 6 7 9 +55 55 57 58 59 62 65 68 +22 25 27 28 30 31 32 29 \ No newline at end of file