Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct Equation { uint64_t target; int size; int nums[12]; } typedef Equation;
- uint64_t combine_numbers(uint64_t num1, uint64_t num2) {
- int digits = 0, multiplier = 1;
- uint64_t temp = num2;
- do { temp /= 10; digits++; } while (temp > 0); // Find the number of digits in num2
- for (int i = 0; i < digits; i++) { multiplier *= 10; }; // Multiply num1 by 10^digits and add num2
- return num1 * multiplier + num2;
- }
- // Recursive function to find the correct combination
- int find_combination(Equation *eq, int index, uint64_t cur_total, int p2) {
- if (index == eq->size) {
- if (cur_total == eq->target) { return 1; }; // Found a solution
- return 0; // No solution
- };
- // Add the next number with a '+' operator
- uint64_t next_total = cur_total + eq->nums[index];
- if (find_combination(eq, index+1, next_total, p2)) { return 1; };
- // Add the next number with a '*' operator
- next_total = (index != 0) ? cur_total * eq->nums[index] : cur_total + eq->nums[index];
- if (find_combination(eq, index+1, next_total, p2)) { return 1;};
- // Add the next number with a '||' operator
- if (index > 0 && p2){
- next_total = combine_numbers(cur_total, eq->nums[index]);
- if (find_combination(eq, index+1, next_total, p2)) { return 1;};
- };
- return 0; // No solution
- }
- int main() {
- FILE *fptr = fopen("/media/int_drive/Development/c/AoC/2024/07/input.txt", "r");
- if (fptr == NULL) { printf("Failed to open the input file"); return 1; };
- // Count number of lines
- int lines = 0;
- char buf[1024];
- while(fgets(buf, sizeof(buf), fptr)){ ++lines; };
- printf("Total number of lines: %d\n", lines);
- rewind(fptr);
- // Parsing lines
- Equation eq_arr[lines];
- int index = 0, scanned = 0;
- while (fgets(buf, sizeof(buf), fptr)) {
- scanned = sscanf(buf, "%lu: %d %d %d %d %d %d %d %d %d %d %d %d", &(eq_arr[index].target),
- &(eq_arr[index].nums[0]), &(eq_arr[index].nums[1]), &(eq_arr[index].nums[2]), &(eq_arr[index].nums[3]),
- &(eq_arr[index].nums[4]), &(eq_arr[index].nums[5]), &(eq_arr[index].nums[6]), &(eq_arr[index].nums[7]),
- &(eq_arr[index].nums[8]), &(eq_arr[index].nums[9]), &(eq_arr[index].nums[10]), &(eq_arr[index].nums[11]));
- eq_arr[index++].size = scanned-1;
- };
- fclose(fptr);
- // Check parsing results
- //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'); };
- // PART 1
- uint64_t result = 0;
- int good = 0, bad = 0;
- for (int i = 0; i < lines; ++i){
- int index = 0;
- uint64_t cur_total = 0;
- if (find_combination(&eq_arr[i], index, cur_total, 0)){ result += eq_arr[i].target; ++good;
- } else { ++ bad; };
- };
- printf("Sum of possible equations: %lu (good: %d bad: %d)\n", result, good, bad);
- // PART 2
- result = 0, good = 0, bad = 0;
- for (int i = 0; i < lines; ++i){
- int index = 0;
- uint64_t cur_total = 0;
- if (find_combination(&eq_arr[i], index, cur_total, 1)){ result += eq_arr[i].target; ++good;
- } else { ++ bad; };
- };
- printf("Sum of possible equations: %lu (good: %d bad: %d)\n", result, good, bad);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment