aoc-2023/c/01/part_1.c

44 lines
793 B
C

#include <stdio.h>
#include <cs50.h>
#include <string.h>
// The amount of input lines
const int lines = 1000;
int extract_number(char *text);
int main(void) {
int sum = 0;
for (int i = 0; i < lines; i++) {
sum += extract_number(get_string(""));
}
printf("%i\n", sum);
return 0;
}
/* Get the right number from both sides of the string
*/
int extract_number(char *text) {
int length = strlen(text);
int first;
for (int i = 0; i < length; i++) {
char c = text[i];
if (c >= '0' && c <= '9') {
first = c - '0';
break;
}
}
for (int i = 1; i <= length; i++) {
char c = text[length - i];
if (c >= '0' && c <= '9') {
return (first * 10) + (c - '0');
}
}
}