not working. this was harder than expected

This commit is contained in:
Stedd 2024-12-02 23:08:08 +01:00
parent ac17bdf12e
commit a342582f69
2 changed files with 40 additions and 30 deletions

View File

@ -98,30 +98,35 @@ void day02::Calculate()
{ {
decreasing = true; decreasing = true;
} }
} else }
{
if (IsUnsafe(currentNumber, previousNumber, increasing, decreasing))
{
numbers.erase(numbers.begin() + i - 1);
i--;
previousNumber = numbers.at(i - 1);
currentNumber = numbers.at(i);
if (currentNumber > previousNumber)
{ if (IsUnsafe(currentNumber, previousNumber, increasing, decreasing) == 1)
increasing = true; {
} numbers.erase(numbers.begin() + i);
if (currentNumber < previousNumber) if (i >= numbers.size())
{ {
decreasing = true; i--;
} }
if (IsUnsafe(currentNumber, previousNumber, increasing, decreasing))
{ previousNumber = numbers.at(i - 1);
// continue; currentNumber = numbers.at(i);
safe = false;
break;
} // if (currentNumber > previousNumber)
}; // {
// increasing = true;
// }
// if (currentNumber < previousNumber)
// {
// decreasing = true;
// }
if (IsUnsafe(currentNumber, previousNumber, increasing, decreasing) == 1)
{
// continue;
safe = false;
break;
}
} }
@ -135,24 +140,29 @@ void day02::Calculate()
std::cout << safeReports << '\n'; std::cout << safeReports << '\n';
} }
bool day02::IsUnsafe(const int current, const int previous, const bool increasing, const bool decreasing) int day02::IsUnsafe(const int current, const int previous, const bool increasing, const bool decreasing)
{ {
const int diff = abs(current - previous); const int diff = abs(current - previous);
if (current < previous && increasing) if (current < previous && increasing)
{ {
return true; //remove current
return 1;
} }
if (current > previous && decreasing) if (current > previous && decreasing)
{ {
return true; //remove current
return 1;
} }
if (current == previous) if (current == previous)
{ {
return true; //remove current
return 1;
} }
if (diff < 1 || diff > 3) if (diff < 1 || diff > 3)
{ {
return true; //remove current
return 1;
} }
return false; //safe
return 0;
} }

View File

@ -11,7 +11,7 @@ class day02
public: public:
static void Calculate(); static void Calculate();
static bool IsUnsafe(int current, int previous, bool increasing, bool decreasing); static int IsUnsafe(int current, int previous, bool increasing, bool decreasing);
}; };
#endif //DAY02_H #endif //DAY02_H