Committing days 1-4
This commit is contained in:
57
01/01.c
Normal file
57
01/01.c
Normal file
@@ -0,0 +1,57 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user