#include #include #define LINES 1000 int compare(const void *a, const void *b); int main() { int new; int old; int safe = 0; FILE *input; input = fopen("./input", "r"); // read number. Compare next number (higher, lower, +-1-2) // if you get to newline, ++ for (int i = 0; i < LINES; i++) { int rising = 0; int first = 1; fscanf(input, "%d", &old); char nextChar = 0; while ((nextChar = fgetc(input)) && nextChar != '\n') { fscanf(input, "%d", &new); // if difference isnt 1-3 if (abs(new - old) > 3 || abs(new - old) < 1) { while(getc(input) != '\n'); goto loopEnd; } // only for first comparison of row. Set direction. if (first && ((new - old) < 0)) rising = 0; else if (first) rising = 1; // for other comparisons, compare current direction to the previous. if (!first && (rising != !((new - old) < 0))) { // printf("rising: %d while %d - %d is %d\n", rising, new, old, new - old); while(getc(input) != '\n'); goto loopEnd; } // get ready for the next comparisons! first = 0; old = new; } safe++; loopEnd: } printf("%d\n", safe); }