#include #include #include #include #define INC_RIGHT 3 int count_trees(const char *filename) { FILE *file = fopen(filename, "r"); // Include space for newline and string terminator char buffer[64] = { 0 }; // Start is always safe fgets(buffer, 64, file); // strlen includes the newline int width = strchr(buffer, '\n') - buffer; int pos = INC_RIGHT; int hit = 0; while (fgets(buffer, 64, file)) { if (buffer[pos] == '#') { hit++; } pos = (pos + INC_RIGHT) % width; } fclose(file); return hit; } int main(int argc, char *argv[]) { printf("%i", count_trees(argv[1])); }