diff options
Diffstat (limited to 'Advent-of-Code-2022/aoc-6/main.c')
-rw-r--r-- | Advent-of-Code-2022/aoc-6/main.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/Advent-of-Code-2022/aoc-6/main.c b/Advent-of-Code-2022/aoc-6/main.c new file mode 100644 index 0000000..d9cf652 --- /dev/null +++ b/Advent-of-Code-2022/aoc-6/main.c @@ -0,0 +1,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; +} |