aoc

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

2.c (3104B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 #include <ctype.h>
      5 #include <stdbool.h>
      6 
      7 typedef struct {
      8     int line;
      9     int index;
     10     int values[9];
     11 } star;
     12 
     13 int main() {
     14     int LINE_LENGTH = 140;
     15     FILE *inputfile = fopen("./input", "r");
     16     char line[2048] = { 0 };
     17     size_t len = 0;
     18     ssize_t read;
     19     if (inputfile == NULL) {
     20         printf("Input file not found. Exiting.");
     21         exit(1);
     22     }
     23     char **lines = malloc(sizeof(char *) * LINE_LENGTH);
     24     int line_count = 0;
     25     while (fgets(line, 1024, inputfile)) {
     26         lines[line_count] = malloc(sizeof(char) * 2048);
     27         strcpy(lines[line_count], line);
     28         line_count++;
     29     }
     30     int range[3] = { -1, 0, 1 };
     31     star *gears = malloc(sizeof(star) * 4096);
     32     int gear_count = 0;
     33     
     34     int answer = 0;
     35     for (int i = 0; i < LINE_LENGTH; i++) {
     36         int n = 0;
     37         char *line = lines[i];
     38         line[strlen(line) - 1] = '\0';
     39         bool havegear = false;
     40         star gear = (star) { 0, 0 };
     41         for (int j = 0; j < strlen(line); j++) {
     42             if (isdigit(line[j]) != 0) {
     43                 n = n * 10 + line[j] - '0';
     44                 for (int ii = 0; ii < 3; ii++) {
     45                     for (int jj = 0; jj < 3; jj++) {
     46                         if (i + range[ii] >= 0 && i + range[ii] < LINE_LENGTH && j + range[jj] >= 0 && j + range[jj] < strlen(lines[i])) {
     47                             char cc = lines[i + range[ii]][j + range[jj]];
     48                             if (cc != '.' && isdigit(cc) == 0 && cc == '*') {
     49                                 gear = (star) { i + range[ii], j + range[jj], {} };
     50                                 havegear = true;
     51                             }
     52                         }
     53                     }
     54                 }
     55             }
     56             if (j == strlen(lines[i]) - 1 || isdigit(line[j]) == 0) {
     57                 if (havegear) {
     58                     int gearcounter = 0;
     59                     int found_i = -1;
     60                     for (int k = 0; k < gear_count; k++) {
     61                         if (gears[k].line == gear.line && gears[k].index == gear.index) {
     62                             found_i = k;
     63                             break;
     64                         }
     65                     }
     66                     if (found_i == -1) {
     67                         gears[gear_count] = gear;
     68                         gears[gear_count].values[0] = n;
     69                         gear_count++;
     70                     } else {
     71                         int length = 0;
     72                         for (int k = 0; k < 9; k++) {
     73                             if (gears[found_i].values[k] != 0) {
     74                                 length++;
     75                             }
     76                         }
     77                         gears[found_i].values[length] = n;
     78                         length += 1;
     79                         if (length == 2) {
     80                             answer += gears[found_i].values[0] * n;
     81                         }
     82                     }
     83                     havegear = false;
     84                 }
     85                 n = 0;
     86             }
     87         }
     88     }
     89     printf("Sum: %d\n", answer);
     90 }