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

34 lines
697 B
C

#include <stdio.h>
int main()
{
FILE *input;
input = fopen("./input", "r");
char inChar;
char numbers[4];
numbers[2] = '\n';
numbers[3] = '\0';
int numIndex = 0;
int sum = 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);
sum += (numbers[0] - '0') * 10 + (numbers[1] - '0');
numIndex = 0;
numbers[0] = '0';
numbers[1] = '0';
}
}
fclose(input);
printf("%d\n", sum);
}