Vulpes-Vulpeos

AoC 2024 day 7

Dec 7th, 2024
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.43 KB | Source Code | 0 0
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. struct Equation { uint64_t target; int size; int nums[12]; } typedef Equation;
  7.  
  8. uint64_t combine_numbers(uint64_t num1, uint64_t num2) {
  9.     int digits = 0, multiplier = 1;
  10.     uint64_t temp = num2;
  11.     do { temp /= 10; digits++; } while (temp > 0); // Find the number of digits in num2
  12.     for (int i = 0; i < digits; i++) { multiplier *= 10; }; // Multiply num1 by 10^digits and add num2
  13.  
  14.     return num1 * multiplier + num2;
  15. }
  16.  
  17. // Recursive function to find the correct combination
  18. int find_combination(Equation *eq, int index, uint64_t cur_total, int p2) {
  19.     if (index == eq->size) {
  20.         if (cur_total == eq->target) { return 1; }; // Found a solution
  21.         return 0; // No solution
  22.     };
  23.     // Add the next number with a '+' operator
  24.     uint64_t next_total = cur_total + eq->nums[index];
  25.     if (find_combination(eq, index+1, next_total, p2)) { return 1; };
  26.  
  27.     // Add the next number with a '*' operator
  28.     next_total = (index != 0) ? cur_total * eq->nums[index] : cur_total + eq->nums[index];
  29.     if (find_combination(eq, index+1, next_total, p2)) { return 1;};
  30.  
  31.     // Add the next number with a '||' operator
  32.     if (index > 0 && p2){
  33.         next_total = combine_numbers(cur_total, eq->nums[index]);
  34.         if (find_combination(eq, index+1, next_total, p2)) { return 1;};
  35.     };
  36.  
  37.     return 0; // No solution
  38. }
  39.  
  40. int main() {
  41.     FILE *fptr = fopen("/media/int_drive/Development/c/AoC/2024/07/input.txt", "r");
  42.     if (fptr == NULL) { printf("Failed to open the input file"); return 1; };
  43.    
  44.     // Count number of lines
  45.     int lines = 0;
  46.     char buf[1024];
  47.     while(fgets(buf, sizeof(buf), fptr)){ ++lines; };
  48.     printf("Total number of lines: %d\n", lines);
  49.     rewind(fptr);
  50.    
  51.     // Parsing lines
  52.     Equation eq_arr[lines];
  53.     int index = 0, scanned = 0;
  54.     while (fgets(buf, sizeof(buf), fptr)) {
  55.         scanned = sscanf(buf, "%lu: %d %d %d %d %d %d %d %d %d %d %d %d", &(eq_arr[index].target),
  56.                  &(eq_arr[index].nums[0]), &(eq_arr[index].nums[1]), &(eq_arr[index].nums[2]),  &(eq_arr[index].nums[3]),
  57.                  &(eq_arr[index].nums[4]), &(eq_arr[index].nums[5]), &(eq_arr[index].nums[6]),  &(eq_arr[index].nums[7]),
  58.                  &(eq_arr[index].nums[8]), &(eq_arr[index].nums[9]), &(eq_arr[index].nums[10]), &(eq_arr[index].nums[11]));
  59.         eq_arr[index++].size = scanned-1;
  60.     };
  61.     fclose(fptr);
  62.     // Check parsing results
  63.     //for (int i = 0; i < lines; ++i){ printf("target: %d | ", eq_arr[i].target); for (int j = 0; j < eq_arr[i].size; ++j){printf("%d ", eq_arr[i].nums[j]); }; putchar('\n'); };
  64.    
  65.     // PART 1
  66.     uint64_t result = 0;
  67.     int good = 0, bad = 0;
  68.     for (int i = 0; i < lines; ++i){
  69.         int index = 0;
  70.         uint64_t cur_total = 0;
  71.         if (find_combination(&eq_arr[i], index, cur_total, 0)){ result += eq_arr[i].target; ++good;
  72.         } else { ++ bad; };
  73.     };
  74.  
  75.     printf("Sum of possible equations: %lu (good: %d bad: %d)\n", result, good, bad);
  76.  
  77.     // PART 2
  78.     result = 0, good = 0, bad = 0;
  79.     for (int i = 0; i < lines; ++i){
  80.         int index = 0;
  81.         uint64_t cur_total = 0;
  82.         if (find_combination(&eq_arr[i], index, cur_total, 1)){ result += eq_arr[i].target; ++good;
  83.         } else { ++ bad; };
  84.     };
  85.  
  86.     printf("Sum of possible equations: %lu (good: %d bad: %d)\n", result, good, bad);
  87.  
  88.     return 0;
  89. }
  90.  
Tags: AoC2024
Advertisement
Add Comment
Please, Sign In to add comment