aoc/2015/day05/answer02.c

48 lines
1.0 KiB
C
Raw Normal View History

2024-07-05 19:55:39 -04:00
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
int main() {
//load input string
FILE *file = fopen("input", "r");
if (file == NULL) {
perror("main: error opening file.");
return -1;
}
uint32_t ncount = 0;
uint8_t two_repeating_check = 0;
uint8_t two_separated_check = 0;
char input[256];
while (fgets(input, sizeof(input), file) != NULL) {
//two letters repeating test
two_repeating_check = 0;
for (uint8_t i = 0; i < strlen(input) - 1; i++) {
for (uint8_t j = i + 2; j < strlen(input) - 1; j++) {
if (input[i] == input[j] && input[i+1] == input[j+1])
two_repeating_check = 1;
}
}
//two letters separated by one test
two_separated_check = 0;
for (uint8_t i = 0; i < strlen(input) - 1; i++) {
if (input[i] == input[i+2])
two_separated_check = 1;
}
if (two_repeating_check && two_separated_check)
ncount++;
}
if (ferror(file)) {
perror("main: error reading file.");
return -1;
}
printf("ncount: %i\n", ncount);
return 0;
}