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()
|
void day02::Calculate()
|
||||||
{
|
{
|
||||||
std::ifstream input("input/day02short.txt");
|
//std::ifstream input("input/day02short.txt");
|
||||||
//std::ifstream input("input/day02.txt");
|
std::ifstream input("input/day02.txt");
|
||||||
|
|
||||||
if (!input)
|
if (!input)
|
||||||
{
|
{
|
||||||
|
@ -71,7 +71,6 @@ void day02::Calculate()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//int num1, num2, num3, num4, num5, num6, num7, num8;
|
|
||||||
|
|
||||||
int safeReports = 0;
|
int safeReports = 0;
|
||||||
bool increasing, decreasing, safe;
|
bool increasing, decreasing, safe;
|
||||||
|
@ -88,7 +87,6 @@ void day02::Calculate()
|
||||||
for (int i = 1; i < numbers.size(); i++)
|
for (int i = 1; i < numbers.size(); i++)
|
||||||
{
|
{
|
||||||
int currentNumber = numbers.at(i);
|
int currentNumber = numbers.at(i);
|
||||||
int diff = abs(currentNumber - previousNumber);
|
|
||||||
|
|
||||||
if (i == 1)
|
if (i == 1)
|
||||||
{
|
{
|
||||||
|
@ -100,32 +98,33 @@ void day02::Calculate()
|
||||||
{
|
{
|
||||||
decreasing = true;
|
decreasing = true;
|
||||||
}
|
}
|
||||||
if (currentNumber == previousNumber)
|
|
||||||
{
|
|
||||||
safe=false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else
|
} else
|
||||||
{
|
{
|
||||||
if (currentNumber < previousNumber && increasing)
|
if (IsUnsafe(currentNumber, previousNumber, increasing, decreasing))
|
||||||
{
|
{
|
||||||
safe=false;
|
numbers.erase(numbers.begin() + i - 1);
|
||||||
break;
|
i--;
|
||||||
}
|
previousNumber = numbers.at(i - 1);
|
||||||
if (currentNumber > previousNumber && decreasing)
|
currentNumber = numbers.at(i);
|
||||||
{
|
|
||||||
safe=false;
|
if (currentNumber > previousNumber)
|
||||||
|
{
|
||||||
|
increasing = true;
|
||||||
|
}
|
||||||
|
if (currentNumber < previousNumber)
|
||||||
|
{
|
||||||
|
decreasing = true;
|
||||||
|
}
|
||||||
|
if (IsUnsafe(currentNumber, previousNumber, increasing, decreasing))
|
||||||
|
{
|
||||||
|
// continue;
|
||||||
|
safe = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (diff < 1 || diff > 3)
|
|
||||||
{
|
|
||||||
safe=false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
previousNumber = currentNumber;
|
previousNumber = currentNumber;
|
||||||
}
|
}
|
||||||
if (safe)
|
if (safe)
|
||||||
|
@ -134,8 +133,26 @@ void day02::Calculate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::cout << safeReports << '\n';
|
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:
|
public:
|
||||||
static void Calculate();
|
static void Calculate();
|
||||||
|
|
||||||
|
static bool IsUnsafe(int current, int previous, bool increasing, bool decreasing);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //DAY02_H
|
#endif //DAY02_H
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
|
1 3 2 4 5
|
||||||
|
8 6 4 4 1
|
||||||
7 6 4 2 1
|
7 6 4 2 1
|
||||||
1 2 7 8 9
|
1 2 7 8 9
|
||||||
9 7 6 2 1
|
9 7 6 2 1
|
||||||
1 3 2 4 5
|
|
||||||
8 6 4 4 1
|
|
||||||
1 3 6 7 9
|
1 3 6 7 9
|
Loading…
Reference in New Issue