Day11 tree

This commit is contained in:
dobiadi
2024-12-13 15:56:59 +01:00
parent 9d0085321d
commit 320ec78949
2 changed files with 52 additions and 57 deletions

View File

Binary file not shown.

View File

@@ -2,6 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <omp.h>
#define MAX_CHARS 1024 #define MAX_CHARS 1024
#define MAX_INITIAL_STONES 1024 #define MAX_INITIAL_STONES 1024
@@ -9,18 +10,14 @@
typedef struct stone { typedef struct stone {
uint64_t value; uint64_t value;
struct stone *prev; struct stone *leafs[2];
struct stone *next; int leaf_count;
} stone_t; } stone_t;
void deallocate_ll(stone_t *leaf); void add_leaf(stone_t* root, stone_t* leaf);
void iterate_ll(stone_t *, void (*cb)(stone_t*, void*), void* ctx); uint64_t call_on_leaf(stone_t *leaf, void (*cb)(stone_t*, void*), void* ctx);
void display(stone_t *stone, void* ctx) {
printf("%lu ", stone->value);
}
void blink(stone_t *leaf, void* ctx); void blink(stone_t *leaf, void* ctx);
void count(stone_t *leaf, void* ctx);
int main() { int main() {
char c, *p, *buf; char c, *p, *buf;
@@ -46,59 +43,62 @@ int main() {
i++; i++;
} }
stone_t *head; free(buf);
for (int i = 0; i < stone_count; i++) { //call_on_leaf(root, display, NULL);
//printf("\n");
uint64_t sum = 0;
#pragma omp parallel for
for (int j = 0; j < stone_count; j++) {
int blink_count = 75;
stone_t *stone = calloc(1, sizeof(stone[0])); stone_t *stone = calloc(1, sizeof(stone[0]));
stone->value = stones[i]; stone->value = stones[j];
if (i == 0) { uint64_t s = call_on_leaf(stone, blink, &blink_count);
head = stone; #pragma omp critical
continue; {
sum += s;
} }
stone_t *tmp = head;
while(tmp->next != NULL) tmp = tmp->next;
tmp->next = stone;
stone->prev = tmp;
} }
free(stones); free(stones);
free(buf);
int blink_count = 75; printf("%lu\n", sum);
for (int i = 0; i < blink_count; i++) {
iterate_ll(head, blink, NULL);
while(head->prev != NULL) head = head->prev;
printf("%i\n", i);
}
uint64_t cnt = 0;
iterate_ll(head, count, &cnt);
printf("%lu\n", cnt);
deallocate_ll(head);
} }
void deallocate_ll(stone_t *leaf) { void add_leaf(stone_t* root, stone_t* leaf) {
if (leaf == NULL) { root->leafs[root->leaf_count++] = leaf;
return;
}
stone_t* next = leaf->next;
free(leaf);
deallocate_ll(next);
} }
void iterate_ll(stone_t *leaf, void (*cb)(stone_t*, void*), void* ctx) { uint64_t call_on_leaf(stone_t *leaf, void (*cb)(stone_t*, void*), void* ctx) {
if (leaf == NULL) { //printf("%lu\n", leaf->value);
return;
}
cb(leaf, ctx); cb(leaf, ctx);
iterate_ll(leaf->next, cb, ctx);
if (*(int*)ctx == 0) {
for (int i = 0; i < leaf->leaf_count; i++) {
free(leaf->leafs[i]);
}
free(leaf);
return 1;
}
uint64_t sum = 0;
(*(int*)ctx)--;
for (int i = 0; i < leaf->leaf_count; i++) {
sum += call_on_leaf(leaf->leafs[i], cb, ctx);
}
(*(int*)ctx)++;
free(leaf);
return sum;
} }
void blink(stone_t *leaf, void* ctx) { void blink(stone_t *leaf, void* ctx) {
if (leaf->value == 0) { if (leaf->value == 0) {
leaf->value = 1; stone_t *next = calloc(1, sizeof(next[0]));
next->value = 1;
add_leaf(leaf, next);
} else { } else {
// Split in two // Split in two
char buf[64]; char buf[64];
@@ -112,20 +112,15 @@ void blink(stone_t *leaf, void* ctx) {
memcpy(right, buf + len / 2, len / 2); memcpy(right, buf + len / 2, len / 2);
right[len / 2] = '\0'; right[len / 2] = '\0';
stone_t *left_stone = calloc(1, sizeof(left_stone[0])); stone_t *left_stone = calloc(1, sizeof(left_stone[0]));
stone_t *right_stone = calloc(1, sizeof(right_stone[0]));
sscanf(left, "%lu", &left_stone->value); sscanf(left, "%lu", &left_stone->value);
sscanf(right, "%lu", &leaf->value); sscanf(right, "%lu", &right_stone->value);
if (leaf->prev) { add_leaf(leaf, left_stone);
left_stone->prev = leaf->prev; add_leaf(leaf, right_stone);
leaf->prev->next = left_stone;
}
leaf->prev = left_stone;
left_stone->next = leaf;
} else { } else {
leaf->value *= 2024; stone_t *next = calloc(1, sizeof(next[0]));
next->value = leaf->value * 2024;
add_leaf(leaf, next);
} }
} }
} }
void count(stone_t *leaf, void* ctx) {
(*(uint64_t*)ctx)++;
}