aoc/2015/day08/answer01.c

53 lines
984 B
C
Raw Normal View History

2024-07-05 20:35:07 -04:00
#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 s = 0; //string characters
for (int i = 0; i < 256; i++) {
if (line[i] == '\0' || line[i] == '\n')
break;
if (line[i] == '\\' && line[i+1] == '\\') {
i++;
s++;
continue;
}
if (line[i] == '\\' && line[i+1] == '\"') {
i++;
s++;
continue;
}
if (line[i] == '\\' && line[i+1] == 'x')
i = i + 3;
s++;
}
//+2 to account for the beginning and ending quotes still contained in s
total += c - s + 2;
}
if (ferror(file)) {
perror("main: error reading file.");
return -1;
}
fclose(file);
printf("%d\n", total);
return 0;
}