98 lines
No EOL
3.2 KiB
C++
98 lines
No EOL
3.2 KiB
C++
#include <stdio.h>
|
|
#include <list>
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <sstream>
|
|
|
|
using namespace std;
|
|
|
|
enum Direction {
|
|
UP,
|
|
DOWN,
|
|
UNSET
|
|
};
|
|
|
|
int main(){
|
|
string line;
|
|
int safeReports = 0;
|
|
stringstream wholeDoc;
|
|
|
|
ifstream puzzle("input.txt", ifstream::in);
|
|
|
|
while (getline(puzzle, line)) {
|
|
istringstream lineStream(line);
|
|
string numberString;
|
|
list<int> currentLevels;
|
|
while (getline(lineStream, numberString, ' ')) {
|
|
int number = stoi(numberString);
|
|
currentLevels.push_back(number);
|
|
}
|
|
|
|
int numOfLevelsInLine = currentLevels.size();
|
|
int numOfLevelsProcessed = 0;
|
|
auto curListIt = currentLevels.begin();
|
|
curListIt++; //point to second item in list
|
|
auto RevCurListIt = currentLevels.rbegin();
|
|
RevCurListIt++; //points to second to last item
|
|
enum Direction dir;
|
|
bool listDirty =false;
|
|
list<int>changeFromPrevLevel;
|
|
list<int>levelGoesUp;
|
|
list<int>levelGoDown;
|
|
list<int>levelStaysSame;
|
|
int totalChange=0;
|
|
|
|
changeFromPrevLevel.push_front(0);
|
|
auto frontIt = currentLevels.begin();
|
|
frontIt++;
|
|
auto backIt = currentLevels.begin();
|
|
|
|
for(int index = 1;frontIt!=currentLevels.end();index++){
|
|
int change = (*backIt)-(*frontIt);
|
|
totalChange+=change;
|
|
changeFromPrevLevel.push_back(change);
|
|
frontIt++;
|
|
backIt++;
|
|
if (change>0){
|
|
levelGoesUp.push_front(index);
|
|
} else if(change<0){
|
|
levelGoDown.push_front(index);
|
|
} else {
|
|
levelStaysSame.push_front(index);
|
|
}
|
|
}
|
|
float averageChange = totalChange/(currentLevels.size()-1);
|
|
//need to pull the first one to have something to compare to
|
|
|
|
int prevlevel = currentLevels.front();
|
|
currentLevels.pop_front();
|
|
numOfLevelsProcessed++;
|
|
if (prevlevel < currentLevels.front()){
|
|
dir = DOWN;
|
|
} else if (prevlevel > currentLevels.front()){
|
|
dir = UP;
|
|
} else {
|
|
currentLevels.clear();
|
|
}
|
|
|
|
|
|
for (int level = currentLevels.front(); !currentLevels.empty();level=currentLevels.front()){
|
|
if (abs(prevlevel-level)>3) {currentLevels.clear();break;}
|
|
if(prevlevel==level){ currentLevels.clear();break;}
|
|
if(dir==UP&&(prevlevel<currentLevels.front())) {currentLevels.clear();break;}
|
|
if(dir==DOWN&&(prevlevel>currentLevels.front())) {currentLevels.clear();break;}
|
|
|
|
prevlevel=currentLevels.front();
|
|
currentLevels.pop_front();
|
|
numOfLevelsProcessed++;
|
|
|
|
if(numOfLevelsProcessed==numOfLevelsInLine){
|
|
safeReports++;
|
|
}
|
|
}
|
|
}
|
|
cout << safeReports;
|
|
puzzle.close();
|
|
} |