From 11f605a2168428713d5f2ae5584a7ba9cf659fdc Mon Sep 17 00:00:00 2001 From: Xamora Date: Wed, 11 Dec 2024 15:37:41 +0100 Subject: [PATCH] finaly... --- 11/part2.c | 77 +++++++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/11/part2.c b/11/part2.c index e7dd05f..50aa8c9 100644 --- a/11/part2.c +++ b/11/part2.c @@ -1,52 +1,62 @@ #include "_.h" #include -typedef struct structure { - int nbr; - struct structure *next; -} struc; - -typedef struct data_t { +typedef struct pair { long nbr; - int max; -} data; + long max; +} pair_t; -long long lenght_struc(struc *head) { - long long count = 0; - while (head->next != NULL) { - head = head->next; - count++; - } - return count; +typedef struct stones { + pair_t pair; + long key; +} stones_t; + +stones_t **stones; + +int index_stones(pair_t pair) { + for (int i = 0; stones[i] != NULL; i++) + if (stones[i]->pair.max == pair.max && + stones[i]->pair.nbr == pair.nbr) + return i; + return -1; } -void print_struc(struc *head) { - while (head->next != NULL) { - head = head->next; - printf("%d ", head->nbr); - } +void insert_stones(pair_t pair, long v) { + int i = 0; + for (; stones[i] != NULL; i++); + stones[i] = calloc(1, sizeof(stones_t)); + stones[i]->pair = pair; + stones[i]->key = v; } -long count = 0; - -void blink(long nbr, int max) { +long blink(long nbr, int max) { + pair_t pair = {nbr, max}; + int i = index_stones(pair); + if (i != -1) + return stones[i]->key; + long v = 1; for (; max > 0; max--) { if (nbr == 0) { nbr = 1; + continue; } else if (((int)floor(log10(ABS(nbr))) + 1) % 2 == 0) { double len = floor(log10(ABS(nbr))) + 1; int left = nbr / (pow(10, len / 2)); + v += blink(left, max - 1); + nbr = nbr % (long)(pow(10, len / 2)); - blink(left, max - 1); + continue; } else nbr *= 2024; } - count++; + insert_stones(pair, v); + return v; } + int main(int argc, char **argv) { if (argc != 2) @@ -54,20 +64,17 @@ int main(int argc, char **argv) { char *input = argv[1]; char **inputs = str_split(strdup(input), ' '); - struc *head = calloc(1, sizeof(struc)); - struc *cur = head; + + long nbrs[1000] = { [0 ... 999] = -1}; for (int i = 0; inputs[i] != NULL; i++) { - cur->nbr = atol(inputs[i]); - cur->next = calloc(1, sizeof(struc)); - cur = cur->next; + nbrs[i] = atol(inputs[i]); } - cur = head; - while (cur->next != NULL) { - blink(cur->nbr, 75); - cur = cur->next; - } + stones = calloc(100000, sizeof(stones_t*)); + long count = 0; + for (long i = 0; nbrs[i] != -1; i++) + count += blink(nbrs[i], 75); printf("count: %ld\n", count);