This repository has been archived on 2024-05-03. You can view files and clone it, but cannot push or open issues or pull requests.
aoc2023-day1/src/calibration.c
Hippolyte Chauvin ad5afe8c01 Modularisation
2023-12-04 11:39:25 +01:00

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;
}