39 lines
907 B
C
39 lines
907 B
C
#include <ctype.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "string.h"
|
|
#include "io.h"
|
|
|
|
static char first_digit(char *line) {
|
|
while (*line != '\0' && !isdigit(*line)) {
|
|
line++;
|
|
}
|
|
|
|
if (*line != '\0') {
|
|
return *line;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
unsigned int get_calibration_sum(FILE *input_file) {
|
|
unsigned int sum = 0;
|
|
char *current_line, *reversed_line;
|
|
char intermediate_buffer[3];
|
|
intermediate_buffer[2] = '\0';
|
|
|
|
while (!feof(input_file)) {
|
|
current_line = read_line(input_file);
|
|
reversed_line = strrev(current_line);
|
|
if (*current_line != '\0') {
|
|
intermediate_buffer[0] = first_digit(current_line);
|
|
intermediate_buffer[1] = first_digit(reversed_line);
|
|
sum += atoi(intermediate_buffer);
|
|
}
|
|
free(reversed_line);
|
|
free(current_line);
|
|
}
|
|
|
|
return sum;
|
|
}
|