added safe report checks
This commit is contained in:
parent
72503af66b
commit
d152641c23
|
@ -6,6 +6,8 @@
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <iterator>
|
||||||
|
#include <vector>
|
||||||
#include <bits/fs_fwd.h>
|
#include <bits/fs_fwd.h>
|
||||||
#include <bits/fs_path.h>
|
#include <bits/fs_path.h>
|
||||||
|
|
||||||
|
@ -58,7 +60,7 @@ 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/day02.txt");
|
std::ifstream input("input/day02.txt");
|
||||||
|
|
||||||
if (!input)
|
if (!input)
|
||||||
|
@ -69,4 +71,71 @@ void day02::Calculate()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//int num1, num2, num3, num4, num5, num6, num7, num8;
|
||||||
|
|
||||||
|
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<int> numbers(std::istream_iterator<int>{iss}, std::istream_iterator<int>());
|
||||||
|
int previousNumber = numbers.at(0);
|
||||||
|
increasing = false;
|
||||||
|
decreasing = false;
|
||||||
|
safe = true;
|
||||||
|
for (int i = 1; i < numbers.size(); i++)
|
||||||
|
{
|
||||||
|
int currentNumber = numbers.at(i);
|
||||||
|
int diff = abs(currentNumber - previousNumber);
|
||||||
|
|
||||||
|
if (i == 1)
|
||||||
|
{
|
||||||
|
if (currentNumber > previousNumber)
|
||||||
|
{
|
||||||
|
increasing = true;
|
||||||
|
}
|
||||||
|
if (currentNumber < previousNumber)
|
||||||
|
{
|
||||||
|
decreasing = true;
|
||||||
|
}
|
||||||
|
if (currentNumber == previousNumber)
|
||||||
|
{
|
||||||
|
safe=false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
if (currentNumber < previousNumber && increasing)
|
||||||
|
{
|
||||||
|
safe=false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (currentNumber > previousNumber && decreasing)
|
||||||
|
{
|
||||||
|
safe=false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (diff < 1 || diff > 3)
|
||||||
|
{
|
||||||
|
safe=false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
previousNumber = currentNumber;
|
||||||
|
}
|
||||||
|
if (safe)
|
||||||
|
{
|
||||||
|
safeReports++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout << safeReports << '\n';
|
||||||
|
// while (std::getline(input, line, '\n') )
|
||||||
|
// {
|
||||||
|
// // printf("%d\n", num2);
|
||||||
|
// }
|
||||||
}
|
}
|
|
@ -10,3 +10,6 @@
|
||||||
29 32 32 33 36 39 40
|
29 32 32 33 36 39 40
|
||||||
72 74 74 75 74
|
72 74 74 75 74
|
||||||
11 14 14 17 17
|
11 14 14 17 17
|
||||||
|
7 6 4 2 1
|
||||||
|
1 3 6 7 9
|
||||||
|
8 6 4 4 1
|
Loading…
Reference in New Issue