finaly...

This commit is contained in:
Xamora 2024-12-11 15:37:41 +01:00
parent a72488734c
commit 11f605a216

View file

@ -1,52 +1,62 @@
#include "_.h"
#include <pthread.h>
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);