111 lines
No EOL
3.9 KiB
C++
111 lines
No EOL
3.9 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 dirSet =false;
|
|
bool listDirty =false;
|
|
|
|
|
|
//check first number, second number and last number to see if they all acend or deced
|
|
//if not check second number, third number and last number to see if they accend or decend
|
|
//then check first number, second number and second to last to see if they accend or decend
|
|
|
|
//set direction
|
|
if ((currentLevels.front()>currentLevels.back())&&(*curListIt) > currentLevels.back()){
|
|
dir = DOWN;
|
|
dirSet=true;
|
|
} else if((currentLevels.front()<currentLevels.back())&&((*curListIt)<currentLevels.back())){
|
|
dir= UP;
|
|
dirSet=true;
|
|
}
|
|
//check ends first.
|
|
if(!dirSet){
|
|
int secondItem = (*curListIt);
|
|
curListIt++; //points to thirdlist item from front
|
|
int secondToLastItem = (*RevCurListIt);
|
|
RevCurListIt++;//points to 3rd to last item
|
|
if((secondItem>(*curListIt)&&(secondItem>currentLevels.back()))){
|
|
dir=DOWN;
|
|
dirSet=true;
|
|
currentLevels.pop_front();//remove first item
|
|
listDirty=true;
|
|
} else if((secondItem<(*curListIt)&&(secondItem<currentLevels.back()))){
|
|
dir = UP;
|
|
dirSet= true;
|
|
currentLevels.pop_front();
|
|
listDirty=true;
|
|
} else if ((currentLevels.front()>currentLevels.back()&&(currentLevels.front()>secondToLastItem))){
|
|
dir = DOWN;
|
|
dirSet= true;
|
|
}
|
|
//check if the back item is the problem
|
|
|
|
}
|
|
|
|
//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();
|
|
} |