diff --git a/11/' b/11/' new file mode 100644 index 0000000..0f9d26a --- /dev/null +++ b/11/' @@ -0,0 +1,92 @@ +#include +#include +#include +#include + +struct list { + int num; + struct list *next; +}; + +bool even_digits(int x) +{ + int i = 0; + while (x /= 10) + i++; + return i % 2; +} + +void split_stone(struct list *stones) { + int digits = 0; + int x = stones->num; + while (x /= 10) + digits++; + + int a = stones->num; + int b = 0; + for (int i = 0; i < digits; i++) { + b = b * 10 + a % 10; + a /= 10; + } + + struct list *tmp = stones->next; + stones->next = malloc(sizeof(struct list)); + stones->num = a; + stones->next->num = b; + stones->next->next = tmp; +} + +int main () +{ + // figure out data dimensions + FILE *input = fopen("./inputDemo", "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); + + // put stones into list + struct list *stones = malloc(sizeof(struct list)); + stones->num = array[0]; + stones->next = NULL; + struct list *current = stones; + for (int i = 1; array[i] != -1; i++) { + while (current->next != NULL) + current = current->next; + current->next = malloc(sizeof(struct list)); + current->next->next = NULL; + current->next->num = array[i]; + } + + // loop through list + for (int i = 0; i < 6; i++) { + current = stones; + while (current) { + // 0 => make 1 + if (current->num == 0) + current->num = 1; + // even number of digits => split, fix list shit + else if (even_digits(current->num)) + split_stone(current); + // (point first split onto second split, second split onto old ptr) + // else => mult 2024 + else + current->num *= 2024; + + current = current->next; + } + } + current = stones; + int sum = 0; + printf("Stones: "); + while (current) { + printf("%d ", current->num); + sum++; + current = current->next; + } + printf("%d\n", sum); +} diff --git a/11/a.out b/11/a.out new file mode 100755 index 0000000..c18907f Binary files /dev/null and b/11/a.out differ diff --git a/11/input b/11/input new file mode 100644 index 0000000..46fe4cc --- /dev/null +++ b/11/input @@ -0,0 +1 @@ +510613 358 84 40702 4373582 2 0 1584 diff --git a/11/inputDemo b/11/inputDemo new file mode 100644 index 0000000..9b26c84 --- /dev/null +++ b/11/inputDemo @@ -0,0 +1 @@ +125 17 diff --git a/11/main.c b/11/main.c new file mode 100644 index 0000000..dce1f15 --- /dev/null +++ b/11/main.c @@ -0,0 +1,116 @@ +#include +#include +#include +#include + +struct list { + unsigned long int num; + struct list *next; +}; + +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(struct list *stones) { + int digits = 1; + unsigned long int x = stones->num; + while (x /= 10) + digits++; + + unsigned long int a = stones->num; + int b = 0; + for (int i = 0; i < digits / 2; i++) { + b += (a % 10) * pow(10, i); + a /= 10; + } + + struct list *tmp = stones->next; + stones->next = malloc(sizeof(struct list)); + stones->num = a; + stones->next->num = b; + stones->next->next = tmp; +} + +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); + + // put stones into list + struct list *stones = malloc(sizeof(struct list)); + stones->num = array[0]; + stones->next = NULL; + struct list *current = stones; + for (int i = 1; array[i] != -1; i++) { + while (current->next != NULL) + current = current->next; + current->next = malloc(sizeof(struct list)); + current->next->next = NULL; + current->next->num = array[i]; + } + + // loop through list + for (int i = 0; i < 75; i++) { + current = stones; + bool last = false; + while (current) { + if (current->next == NULL) + last = true; + // 0 => make 1 + if (current->num == 0) + current->num = 1; + // even number of digits => split, fix list shit + else if (even_digits(current->num)) { + split_stone(current); + current = current->next; + } + // (point first split onto second split, second split onto old ptr) + // else => mult 2024 + else + current->num *= 2024; + + current = current->next; + if (last) + break; + } + struct list *tmp = stones; + /* + int sum = 0; + printf("%d blinks: \n", i + 1); + while (tmp) { + printf("%lu ", tmp->num); + sum++; + tmp = tmp->next; + } + printf("\ntotal: %d\n", sum); + */ + } + current = stones; + unsigned long int sum = 0; + printf("Stones: "); + while (current) { + printf("%lu ", current->num); + sum++; + current = current->next; + } + printf("\ntotal: %d\n", sum); +}