it's not enough to always correct to remove the previous number.
This commit is contained in:
parent
5f5728a6a5
commit
1231d99316
|
@ -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)
|
||||
{
|
||||
safe=false;
|
||||
break;
|
||||
}
|
||||
if (currentNumber > previousNumber && decreasing)
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -10,6 +10,8 @@ class day02
|
|||
{
|
||||
public:
|
||||
static void Calculate();
|
||||
|
||||
static bool IsUnsafe(int current, int previous, bool increasing, bool decreasing);
|
||||
};
|
||||
|
||||
#endif //DAY02_H
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue