From 1231d993167cdc4b25d0bd14891ceba92dd907fc Mon Sep 17 00:00:00 2001 From: Stedd Date: Mon, 2 Dec 2024 22:04:04 +0100 Subject: [PATCH] it's not enough to always correct to remove the previous number. --- days/day02.cpp | 73 +++++++++++++++++++++++++++----------------- days/day02.h | 2 ++ input/day02short.txt | 4 +-- 3 files changed, 49 insertions(+), 30 deletions(-) diff --git a/days/day02.cpp b/days/day02.cpp index db074b3..f7385f5 100644 --- a/days/day02.cpp +++ b/days/day02.cpp @@ -60,8 +60,8 @@ Analyze the unusual data from the engineers. How many reports are safe? void day02::Calculate() { - std::ifstream input("input/day02short.txt"); - //std::ifstream input("input/day02.txt"); + //std::ifstream input("input/day02short.txt"); + std::ifstream input("input/day02.txt"); if (!input) { @@ -71,7 +71,6 @@ void day02::Calculate() return; } - //int num1, num2, num3, num4, num5, num6, num7, num8; int safeReports = 0; bool increasing, decreasing, safe; @@ -88,7 +87,6 @@ void day02::Calculate() for (int i = 1; i < numbers.size(); i++) { int currentNumber = numbers.at(i); - int diff = abs(currentNumber - previousNumber); if (i == 1) { @@ -100,32 +98,33 @@ void day02::Calculate() { decreasing = true; } - if (currentNumber == previousNumber) - { - safe=false; - break; - } } else { - if (currentNumber < previousNumber && increasing) + if (IsUnsafe(currentNumber, previousNumber, increasing, decreasing)) { - safe=false; - break; - } - if (currentNumber > previousNumber && decreasing) - { - safe=false; - break; - } + numbers.erase(numbers.begin() + i - 1); + i--; + previousNumber = numbers.at(i - 1); + currentNumber = numbers.at(i); + + if (currentNumber > previousNumber) + { + increasing = true; + } + if (currentNumber < previousNumber) + { + decreasing = true; + } + if (IsUnsafe(currentNumber, previousNumber, increasing, decreasing)) + { + // continue; + safe = false; + break; + } + }; } - if (diff < 1 || diff > 3) - { - safe=false; - break; - } - previousNumber = currentNumber; } if (safe) @@ -134,8 +133,26 @@ void day02::Calculate() } } std::cout << safeReports << '\n'; - // while (std::getline(input, line, '\n') ) - // { - // // printf("%d\n", num2); - // } +} + +bool day02::IsUnsafe(const int current, const int previous, const bool increasing, const bool decreasing) +{ + const int diff = abs(current - previous); + if (current < previous && increasing) + { + return true; + } + if (current > previous && decreasing) + { + return true; + } + if (current == previous) + { + return true; + } + if (diff < 1 || diff > 3) + { + return true; + } + return false; } diff --git a/days/day02.h b/days/day02.h index 204a635..4de090a 100644 --- a/days/day02.h +++ b/days/day02.h @@ -10,6 +10,8 @@ class day02 { public: static void Calculate(); + + static bool IsUnsafe(int current, int previous, bool increasing, bool decreasing); }; #endif //DAY02_H diff --git a/input/day02short.txt b/input/day02short.txt index 82cd679..12f4045 100644 --- a/input/day02short.txt +++ b/input/day02short.txt @@ -1,6 +1,6 @@ +1 3 2 4 5 +8 6 4 4 1 7 6 4 2 1 1 2 7 8 9 9 7 6 2 1 -1 3 2 4 5 -8 6 4 4 1 1 3 6 7 9 \ No newline at end of file