aoc/2015/day08/answer02.c
2024-07-05 20:35:07 -04:00

44 lines
812 B
C

#include <stdio.h>
#include <stdint.h>
int main() {
FILE *file = fopen("input", "r");
if (file == NULL) {
perror("main: error opening file.");
return -1;
}
int32_t total = 0;
char line[256];
while (fgets(line, sizeof(line), file) != NULL) {
int32_t c = 0; //code characters
for (int i = 0; i < 256; i++) {
if (line[i] == '\0' || line[i] == '\n')
break;
c++;
}
int32_t e = 0; //newly encoded characters
for (int i = 0; i < 256; i++) {
if (line[i] == '\0' || line[i] == '\n')
break;
if (line[i] == '\"' || line[i] == '\\')
e++;
e++;
}
e = e + 2; //+2 to account for the beginning and ending quotes
total = total + e - c;
}
if (ferror(file)) {
perror("main: error reading file.");
return -1;
}
fclose(file);
printf("%i\n", total);
return 0;
}