advent-of-code/2018/05/day5.c

47 lines
1.0 KiB
C
Raw Permalink Normal View History

#include <stdio.h>
#include <stdlib.h>
2018-12-05 21:21:46 +01:00
#include <sys/stat.h>
2018-12-05 18:52:24 +01:00
int isSameLetter(char a, char b) {
char diff = (a > b) ? a - b : b - a;
return diff == 32;
}
2018-12-05 21:21:46 +01:00
int processArray(char* input, int length) {
2018-12-05 18:52:24 +01:00
int outputLength = 0;
2018-12-05 21:57:07 +01:00
for (int i=0; i<length; i++) {
2018-12-05 18:52:24 +01:00
if (isSameLetter(input[i], input[i+1]) == 1) {
i++;
} else {
2018-12-05 21:21:46 +01:00
input[outputLength] = input[i];
2018-12-05 18:52:24 +01:00
outputLength++;
}
}
2018-12-05 21:57:07 +01:00
return outputLength;
2018-12-05 18:52:24 +01:00
}
int main() {
2018-12-05 21:21:46 +01:00
struct stat stbuffer;
FILE *inputFile = fopen("input", "r");
2018-12-05 21:57:07 +01:00
fstat(fileno(inputFile), &stbuffer);
int inSize = stbuffer.st_size + 1;
//inSize = 20;
//fseek(inputFile, 10020, 0);
2018-12-05 21:21:46 +01:00
char *input = malloc(inSize);
fgets(input, inSize, inputFile);
int lastLength = inSize;
int outLength = inSize;
do {
lastLength = outLength;
outLength = processArray(input, lastLength);
} while (lastLength != outLength);
2018-12-05 21:57:07 +01:00
printf("%d\n", outLength - 1);
2018-12-05 21:21:46 +01:00
fclose(inputFile);
free(input);
2018-12-05 18:52:24 +01:00
return 0;
}