diff options
-rw-r--r-- | 6.c | 39 | ||||
-rw-r--r-- | 6a.c | 40 |
2 files changed, 30 insertions, 49 deletions
@@ -2,8 +2,6 @@ #include <stdio.h> #include <string.h> -#define MAX_BOARDS 100 - static char *ReadLine(char *p, size_t size, FILE *fp) { if ((p=fgets(p, size, fp))) @@ -22,8 +20,8 @@ static char *ReadLine(char *p, size_t size, FILE *fp) int main(void) { char buff[1024]; - int *num = NULL; - int num_count = 0; + unsigned long long num[9] = {0}; + unsigned long long num_count = 0; char *p = NULL; int f = 0; int cycle = 0; @@ -34,36 +32,31 @@ int main(void) while(p) { - num = realloc(num, (num_count + 1) * sizeof *num); - num[num_count++] = atoi(p); + num[atoi(p)]++; p = strtok(NULL, ","); } for(cycle = 0; cycle < 80; cycle++) { - int to_add = 0; + unsigned long long zero = 0; - for(f = 0; f < num_count; f++) - { - if (num[f] == 0) - { - to_add++; - num[f] = 6; - } - else - { - num[f]--; - } - } + zero = num[0]; - for(f = 0; f < to_add; f++) + for(f=0; f < 8; f++) { - num = realloc(num, (num_count + 1) * sizeof *num); - num[num_count++] = 8; + num[f] = num[f+1]; } + + num[6] += zero; + num[8] = zero; + } + + for(f = 0; f < 9; f++) + { + num_count += num[f]; } - printf("num_count=%d\n", num_count); + printf("num_count=%llu\n", num_count); return 0; } @@ -2,8 +2,6 @@ #include <stdio.h> #include <string.h> -#define MAX_BOARDS 100 - static char *ReadLine(char *p, size_t size, FILE *fp) { if ((p=fgets(p, size, fp))) @@ -22,10 +20,10 @@ static char *ReadLine(char *p, size_t size, FILE *fp) int main(void) { char buff[1024]; - char *num = NULL; + unsigned long long num[9] = {0}; unsigned long long num_count = 0; char *p = NULL; - unsigned long long f = 0; + int f = 0; int cycle = 0; ReadLine(buff, sizeof buff, stdin); @@ -34,38 +32,28 @@ int main(void) while(p) { - num = realloc(num, (num_count + 1) * sizeof *num); - num[num_count++] = atoi(p); + num[atoi(p)]++; p = strtok(NULL, ","); } for(cycle = 0; cycle < 256; cycle++) { - int to_add = 0; + unsigned long long zero = 0; - for(f = 0; f < num_count; f++) - { - if (num[f] == 0) - { - to_add++; - num[f] = 6; - } - else - { - num[f]--; - } - } + zero = num[0]; - if (to_add) + for(f=0; f < 8; f++) { - num = realloc(num, (num_count + to_add) * sizeof *num); - for(f = 0; f < to_add; f++) - { - num[num_count++] = 8; - } + num[f] = num[f+1]; } - printf("cycle/num_count = %d/%llu\n", cycle, num_count); + num[6] += zero; + num[8] = zero; + } + + for(f = 0; f < 9; f++) + { + num_count += num[f]; } printf("num_count=%llu\n", num_count); |