aoc

Advent of code
git clone https://codeberg.org/night0721/aoc
Log | Files | Refs

1.c (961B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 
      5 int main() {
      6     FILE *inputfile = fopen("./input", "r");
      7     char line[100] = { 0 };
      8     int sum = 0;
      9     size_t len = 0;
     10     ssize_t read;
     11     if (inputfile == NULL) {
     12         printf("Input file not found. Exiting.");
     13         exit(1);
     14     }
     15     while (fgets(line, 100, inputfile)) {
     16         int length = strlen(line);
     17         char *concat = malloc(sizeof(char) * length);
     18         for (int i = 0; i < length - 1; i++) {
     19             int currentChar = (int) line[i];
     20             if (currentChar >= 49 && currentChar <= 57) {
     21                 int len = strlen(concat);
     22                 concat[len] = line[i];
     23             }
     24         }
     25         concat[strlen(concat)] = '\0';
     26         char trimmed[3] = { 0 };
     27         trimmed[0] = concat[0];
     28         trimmed[1] = concat[strlen(concat) - 1];
     29         trimmed[2] = '\0';
     30         sum += atoi(trimmed);
     31     }
     32     fclose(inputfile);
     33     printf("Sum: %d\n", sum); 
     34 }