summaryrefslogtreecommitdiff
path: root/Advent-of-Code-2020/AOC-6/main.c
blob: 7fa135e2191d010c9b576c4037d20c7af1c57f44 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if 0
#define PART_1
#else
#define PART_2
#endif

#if 0
#define FILE_PATH "example.txt"
#else
#define FILE_PATH "input.txt"
#endif

int q_answ = 0;

void parse()
{
    FILE *fp = fopen(FILE_PATH, "r");
    if(!fp) {
        fprintf(stderr, "ERROR: Could not open file: %s", FILE_PATH);
        exit(EXIT_FAILURE);
    }

    char questions[26] = "abcdefghijklmnopqrstuvwxyz";
    int answered[26] = {0};

#ifdef PART_2
    int ppl_in_group = 0;
#endif

    char line[32];
    while(fgets(line, sizeof(line), fp) != NULL)
    {
        if(line[0] == '\n' || strncmp(line, "EOF", 3) == 0) {
            for(int i = 0; i < 26; i++) {
            #ifdef PART_1
                q_answ += answered[i];
            #endif

            #ifdef PART_2
                if(answered[i] == ppl_in_group)
                     q_answ++;
            #endif
            }

        #ifdef PART_2
            ppl_in_group = 0;
        #endif
            memset(answered, 0, sizeof(answered));
            continue;
        }

        #ifdef PART_2
            ppl_in_group++;
        #endif

        for(int i = 0; i < strlen(line); i++)
            for(int j = 0; j < 26; j++)
                if(line[i] == questions[j]) {
                #ifdef PART_1
                    answered[j] = 1;
                #endif
                #ifdef PART_2
                    answered[j]++;
                #endif
                }
    }

    fclose(fp);
}

int main(void)
{
    parse();
    printf("%d questions answered\n", q_answ);
    return 0;
}