puzzle1.c (616B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <errno.h> 4 #include <string.h> 5 #include <strings.h> 6 7 #define MAX_LEN 8 8 9 int puzzle1(const char *filename) { 10 FILE *infile = fopen(filename, "r"); 11 if (infile == NULL) { 12 fprintf(stderr, "fopen() error: %s\n", strerror(errno)); 13 return -1; 14 } 15 16 char buf[MAX_LEN] = {0}; 17 int prev_depth = -1; 18 int counter = 0; 19 20 while (fgets(buf, MAX_LEN, infile) != NULL) { 21 int depth = atoi(buf); 22 if (prev_depth > -1 && depth > prev_depth) { 23 ++counter; 24 } 25 prev_depth = depth; 26 bzero(buf, MAX_LEN); 27 } 28 29 // mutiny! ignoring feof/ferror. 30 31 fclose(infile); 32 return counter; 33 }