diff options
author | Ian C <ianc@noddybox.co.uk> | 2021-12-09 17:15:34 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2021-12-09 17:15:34 +0000 |
commit | 414b6d624d40b81748ef67852d6a1255174aa0e5 (patch) | |
tree | 2bc36b774832108f04acd5c4da2d594ee8d8f2b8 | |
parent | 9b81cb663473431254d428983e114674519c18c3 (diff) |
Tried to optimise 6a. Still running too long. Need more patience.
-rw-r--r-- | 6a.c | 56 |
1 files changed, 11 insertions, 45 deletions
@@ -2,14 +2,7 @@ #include <stdio.h> #include <string.h> -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); |