diff options
author | ian.cowburn <ian.cowburn@ianc-work-macbook.local> | 2021-12-09 17:02:18 +0000 |
---|---|---|
committer | ian.cowburn <ian.cowburn@ianc-work-macbook.local> | 2021-12-09 17:02:18 +0000 |
commit | 9b81cb663473431254d428983e114674519c18c3 (patch) | |
tree | e60a7330a9719b9d514363a3cbad13f695ee4fc1 | |
parent | e34fd16b64ebf0ddd7ede60e938935320135d3ee (diff) |
Added day 6. 6a needs some optimisation, or a lote of memory
-rw-r--r-- | 6.c | 69 | ||||
-rw-r--r-- | 6a.c | 108 | ||||
-rw-r--r-- | data.6 | 1 | ||||
-rw-r--r-- | data.6a | 1 |
4 files changed, 179 insertions, 0 deletions
@@ -0,0 +1,69 @@ +#include <stdlib.h> +#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))) + { + size_t l = strlen(p); + + while(l && p[l-1] == '\n') + { + p[--l] = 0; + } + } + + return p; +} + +int main(void) +{ + char buff[1024]; + int *num = NULL; + int num_count = 0; + char *p = NULL; + int f = 0; + int cycle = 0; + + ReadLine(buff, sizeof buff, stdin); + + p = strtok(buff, ","); + + while(p) + { + num = realloc(num, (num_count + 1) * sizeof *num); + num[num_count++] = atoi(p); + p = strtok(NULL, ","); + } + + for(cycle = 0; cycle < 80; cycle++) + { + int to_add = 0; + + for(f = 0; f < num_count; f++) + { + if (num[f] == 0) + { + to_add++; + num[f] = 6; + } + else + { + num[f]--; + } + } + + for(f = 0; f < to_add; f++) + { + num = realloc(num, (num_count + 1) * sizeof *num); + num[num_count++] = 8; + } + } + + printf("num_count=%d\n", num_count); + + return 0; +} @@ -0,0 +1,108 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +typedef struct node +{ + struct node *next; + int i; +} node; + +static node *head = NULL; +static node *tail = NULL; + +static char *ReadLine(char *p, size_t size, FILE *fp) +{ + if ((p=fgets(p, size, fp))) + { + size_t l = strlen(p); + + while(l && p[l-1] == '\n') + { + p[--l] = 0; + } + } + + 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]; + unsigned long long num_count = 0; + char *p = NULL; + unsigned long long f = 0; + int cycle = 0; + + ReadLine(buff, sizeof buff, stdin); + + p = strtok(buff, ","); + + while(p) + { + Add(atoi(p)); + p = strtok(NULL, ","); + } + + for(cycle = 0; cycle < 256; cycle++) + { + int to_add = 0; + node *list; + + list = head; + + while(list) + { + if (list->i == 0) + { + to_add++; + list->i = 6; + } + else + { + list->i--; + } + + list = list->next; + } + + if (to_add) + { + num_count += to_add; + + for(f = 0; f < to_add; f++) + { + Add(8); + } + } + + printf("cycle %d/%llu\n", cycle, num_count); + } + + printf("num_count=%llu\n", num_count); + + return 0; +} @@ -0,0 +1 @@ +3,5,3,1,4,4,5,5,2,1,4,3,5,1,3,5,3,2,4,3,5,3,1,1,2,1,4,5,3,1,4,5,4,3,3,4,3,1,1,2,2,4,1,1,4,3,4,4,2,4,3,1,5,1,2,3,2,4,4,1,1,1,3,3,5,1,4,5,5,2,5,3,3,1,1,2,3,3,3,1,4,1,5,1,5,3,3,1,5,3,4,3,1,4,1,1,1,2,1,2,3,2,2,4,3,5,5,4,5,3,1,4,4,2,4,4,5,1,5,3,3,5,5,4,4,1,3,2,3,1,2,4,5,3,3,5,4,1,1,5,2,5,1,5,5,4,1,1,1,1,5,3,3,4,4,2,2,1,5,1,1,1,4,4,2,2,2,2,2,5,5,2,4,4,4,1,2,5,4,5,2,5,4,3,1,1,5,4,5,3,2,3,4,1,4,1,1,3,5,1,2,5,1,1,1,5,1,1,4,2,3,4,1,3,3,2,3,1,1,4,4,3,2,1,2,1,4,2,5,4,2,5,3,2,3,3,4,1,3,5,5,1,3,4,5,1,1,3,1,2,1,1,1,1,5,1,1,2,1,4,5,2,1,5,4,2,2,5,5,1,5,1,2,1,5,2,4,3,2,3,1,1,1,2,3,1,4,3,1,2,3,2,1,3,3,2,1,2,5,2 @@ -0,0 +1 @@ +3,4,3,1,2 |