blob: b664b7d2a835066e689b9d978bf8f6f1c9e3debb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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[argc - 1]));
}
|