summaryrefslogtreecommitdiff
path: root/Advent-of-Code-2022/aoc-6/main.c
blob: d9cf652054fccdec65f7868202051f1e60f24566 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if 0
  #define PART(line) part(line, 4)
#else
  #define PART(line) part(line, 14)
#endif

#if 0
  #define FILENAME "sample.txt"
#else
  #define FILENAME "input.txt"
#endif

void part(char *line, int n)
{
    n--;
    int times_seen['z'-'a'+1] = {0};

    for(ulong i = n; i < strlen(line) - 1; i++)
    {
        int stop = 0;
        for(ulong j = i-n; j <= i && !stop; j++)
            if(++(times_seen[line[j]-'a']) > 1)
                stop = 1;

        memset(times_seen, 0, sizeof(times_seen));
        if(stop) continue;

        printf("%ld\n", i + 1);
        return;
    }
}

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

    char line[4096];
    while(fgets(line, sizeof(line), fp))
        PART(line);

    fclose(fp);
}

int main(void)
{
    parse();
    return 0;
}