aoc

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

2.c (2972B)


      1 #include "../../lib/util.h"
      2 
      3 #include <stdio.h>
      4 #include <string.h>
      5 #include <stdlib.h>
      6 #include <stdbool.h>
      7 
      8 int main() {
      9     int LINES = 199;
     10     FILE *inputfile = fopen("./input", "r");
     11     char line[2048] = { 0 };
     12     size_t len = 0;
     13     ssize_t read;
     14     if (inputfile == NULL) {
     15         printf("Input file not found. Exiting.");
     16         exit(1);
     17     }
     18     char lines[LINES][2048];
     19     int cards[LINES];
     20     for (int i = 0; i < LINES; i++) {
     21         cards[i] = 0;
     22     }
     23     int line_count = 0;
     24     while (fgets(line, 2048, inputfile)) {
     25         cards[line_count] += 1;
     26         strcpy(lines[line_count], line);
     27         char copied_line[2048];
     28         strcpy(copied_line, line);
     29         char **split = str_split(copied_line, '|');
     30         char *left = split[0];
     31         char *right = split[1];
     32         int *win_num = malloc(sizeof(int) * 1024);
     33         char *winning_numbers = str_split(left, ':')[1];
     34         winning_numbers = trimwhitespace(winning_numbers);
     35         // check consecutive space then shift characters to left
     36         for (int i = 0; i < strlen(winning_numbers); i++) {
     37             if (winning_numbers[i] == ' ' && winning_numbers[i + 1] == ' ') {
     38                 // shift chracters to left
     39                 for (int j = i; j < strlen(winning_numbers); j++) {
     40                     winning_numbers[j] = winning_numbers[j + 1];
     41                 }
     42             }
     43         }
     44 
     45         char **winning_numbers_split = str_split(winning_numbers, ' ');
     46         int winning_count = 0;
     47         while (winning_numbers_split[winning_count] != NULL) {
     48             win_num[winning_count] = atoi(winning_numbers_split[winning_count]);
     49             winning_count++;
     50         }
     51 
     52         int *gambled_num = malloc(sizeof(int) * 1024);
     53         // check consecutive space then shift characters to left
     54         for (int i = 0; i < strlen(right); i++) {
     55             if (right[i] == ' ' && right[i + 1] == ' ') {
     56                 // shift chracters to left
     57                 for (int j = i; j < strlen(right); j++) {
     58                     right[j] = right[j + 1];
     59                 }
     60             }
     61         }
     62         right = trimwhitespace(right);
     63         char **gambled_numbers = str_split(right, ' ');
     64         int gambled_count = 0;
     65         int score = 0;
     66         int matches = 0;
     67         while (gambled_numbers[gambled_count] != NULL) {
     68             int winning_counter = 0;
     69             gambled_num[gambled_count] = atoi(gambled_numbers[gambled_count]);
     70             while (win_num[winning_counter] != NULL) {
     71                 if (gambled_num[gambled_count] == win_num[winning_counter]) {
     72                     matches++;
     73                 }
     74                 winning_counter++;
     75             }
     76             gambled_count++;
     77         }
     78         for (int i = 1; i < matches + 1; i++) {
     79             cards[line_count + i] += cards[line_count];
     80         }
     81         line_count++;
     82     }
     83     int total = 0;
     84     for (int i = 0; i < LINES; i++) {
     85         total += cards[i];
     86     }
     87     printf("Sum: %d\n", total);
     88 }