AoC2023/day1/day1.c
2023-12-03 17:03:15 +01:00

50 lines
1 KiB
C

#include <stdio.h>
int main()
{
FILE *input;
input = fopen("./input", "r");
FILE *output;
output = fopen("./output", "w");
char inChar;
char numbers[4];
numbers[2] = '\n';
numbers[3] = '\0';
int numIndex = 0;
while((inChar = fgetc(input)) != EOF) {
if (inChar >= '0' && inChar <= '9' && numIndex == 0) {
numbers[0] = inChar;
numbers[1] = inChar;
numIndex = 1;
} else if (inChar >= '0' && inChar <= '9' && numIndex == 1) {
numbers[1] = inChar;
} else if (inChar == EOF || inChar == '\n' || inChar == '\0') {
fputs(numbers, output);
numIndex = 0;
numbers[0] = '0';
numbers[1] = '0';
}
}
fclose(input);
fclose(output);
int sum = 0;
input = fopen("./output", "r");
while((inChar = fgetc(input)) != EOF) {
if (inChar >= '0' && inChar <= '9' && numIndex == 0) {
sum += (inChar - '0') * 10;
numIndex = 1;
} else if (inChar >= '0' && inChar <= '9' && numIndex == 1) {
sum += inChar - '0';
numIndex = 0;
} else if (inChar == EOF)
break;
}
printf("%d\n", sum);
fclose(input);
}