57 lines
1.1 KiB
C
57 lines
1.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdint.h>
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
// Get filename as an argument, defaults to "input.txt"
|
|
char *path = (argc > 1) ? argv[1] : "input.txt";
|
|
|
|
if (!path)
|
|
{
|
|
printf("Requires input file.\n");
|
|
return 1;
|
|
}
|
|
|
|
FILE *file = fopen(path, "r");
|
|
|
|
if (!file)
|
|
{
|
|
printf("Could not find file.");
|
|
return 1;
|
|
}
|
|
|
|
int8_t firstdigit = -1;
|
|
int8_t lastdigit = -1;
|
|
uint32_t sum = 0;
|
|
char current = 0;
|
|
|
|
while (current != EOF)
|
|
{
|
|
current = fgetc(file);
|
|
|
|
if (current == '\n')
|
|
{
|
|
if (lastdigit == -1) lastdigit = firstdigit;
|
|
sum += (firstdigit * 10) + lastdigit;
|
|
firstdigit = -1;
|
|
lastdigit = -1;
|
|
continue;
|
|
}
|
|
|
|
if (current < '0' || current > '9') continue;
|
|
|
|
if (firstdigit == -1)
|
|
{
|
|
firstdigit = current - '0';
|
|
}
|
|
else
|
|
{
|
|
lastdigit = current - '0';
|
|
}
|
|
}
|
|
|
|
printf("Sum: %d\n", sum);
|
|
|
|
return 0;
|
|
} |