From 414b6d624d40b81748ef67852d6a1255174aa0e5 Mon Sep 17 00:00:00 2001 From: Ian C Date: Thu, 9 Dec 2021 17:15:34 +0000 Subject: Tried to optimise 6a. Still running too long. Need more patience. --- 6a.c | 56 +++++++++++--------------------------------------------- 1 file changed, 11 insertions(+), 45 deletions(-) diff --git a/6a.c b/6a.c index 2c0031d..da667f2 100644 --- a/6a.c +++ b/6a.c @@ -2,14 +2,7 @@ #include #include -typedef struct node -{ - struct node *next; - int i; -} node; - -static node *head = NULL; -static node *tail = NULL; +#define MAX_BOARDS 100 static char *ReadLine(char *p, size_t size, FILE *fp) { @@ -26,32 +19,10 @@ static char *ReadLine(char *p, size_t size, FILE *fp) return p; } -static void Add(int i) -{ - node *new; - - new = malloc(sizeof *new); - - new->next = NULL; - - if (tail) - { - tail->next = new; - } - - tail = new; - - if (!head) - { - head = new; - } - - new->i = i; -} - int main(void) { char buff[1024]; + char *num = NULL; unsigned long long num_count = 0; char *p = NULL; unsigned long long f = 0; @@ -63,43 +34,38 @@ int main(void) while(p) { - Add(atoi(p)); + num = realloc(num, (num_count + 1) * sizeof *num); + num[num_count++] = atoi(p); p = strtok(NULL, ","); } for(cycle = 0; cycle < 256; cycle++) { int to_add = 0; - node *list; - - list = head; - while(list) + for(f = 0; f < num_count; f++) { - if (list->i == 0) + if (num[f] == 0) { to_add++; - list->i = 6; + num[f] = 6; } else { - list->i--; + num[f]--; } - - list = list->next; } if (to_add) { - num_count += to_add; - + num = realloc(num, (num_count + to_add) * sizeof *num); for(f = 0; f < to_add; f++) { - Add(8); + num[num_count++] = 8; } } - printf("cycle %d/%llu\n", cycle, num_count); + printf("cycle/num_count = %d/%llu\n", cycle, num_count); } printf("num_count=%llu\n", num_count); -- cgit v1.2.3