#include #include #include #include struct list { unsigned long int num; unsigned long int solution; int i; struct list *next; }; struct pair { unsigned long int a; unsigned long int b; }; struct list *solutions; struct list *current; bool even_digits(unsigned long int x) { int i = 0; while (x /= 10) i++; return i % 2; } // power! x to the y! int pow(int x, int y) { int ans = 1; for (int i = 0; i < y; i++) ans *= x; return ans; } void split_stone(unsigned long int stone, struct pair *pair) { int digits = 1; unsigned long int x = stone; while (x /= 10) digits++; unsigned long int a = stone; int b = 0; for (int i = 0; i < digits / 2; i++) { b += (a % 10) * pow(10, i); a /= 10; } pair->a = a; pair->b = b; } unsigned long int solve_stone(unsigned long int stone, int i) { i--; if (i == 0) return 1; // check / add to solved list current = solutions; while (current->next && !(current->num == stone && current->i == i)) { current = current->next; } if (current->num == stone && current->i == i) { printf("ay found repetition\n"); return current->solution; } int sum = 0; // if not solved, split / multiply / set to 1 and send to solve // 0 => make 1 if (stone == 0) sum = solve_stone(1, i); // even number of digits => split, fix list shit else if (even_digits(stone)) { struct pair *pair = malloc(sizeof(struct pair)); split_stone(stone, pair); sum += solve_stone(pair->a, i); sum += solve_stone(pair->b, i); free(pair); } // (point first split onto second split, second split onto old ptr) // else => mult 2024 else sum = solve_stone(stone * 2024, i); while (current->next && current->num != -1) {} current->next = malloc(sizeof(struct list)); current = current->next; current->num = stone; current->i = i; current->solution = sum; return sum; } int main () { // figure out data dimensions FILE *input = fopen("./input", "r"); unsigned int tmp; unsigned int array[10]; for (int i = 0; i < 10; i++) array[i] = -1; for (int i = 0; fscanf(input, "%d", &tmp) == 1; i++) { array[i] = tmp; } fclose(input); solutions = malloc(sizeof(struct list)); solutions->num = -1; solutions->next = NULL; unsigned long int sum = 0; for (int i = 0; array[i] != -1; i++) { sum += solve_stone(array[i], 6); } printf("total: %d\n", sum); }