summaryrefslogtreecommitdiff
path: root/3/part1.c
blob: 19f3bf336d16dee36a12962930e5eb71cad12894 (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
#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;
    }

    return hit;
}

int main()
{
    printf("%i", count_trees("input"));
}