From 9b81cb663473431254d428983e114674519c18c3 Mon Sep 17 00:00:00 2001 From: "ian.cowburn" Date: Thu, 9 Dec 2021 17:02:18 +0000 Subject: Added day 6. 6a needs some optimisation, or a lote of memory --- 6.c | 69 +++++++++++++++++++++++++++++++++++++++++ 6a.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ data.6 | 1 + data.6a | 1 + 4 files changed, 179 insertions(+) create mode 100644 6.c create mode 100644 6a.c create mode 100644 data.6 create mode 100644 data.6a diff --git a/6.c b/6.c new file mode 100644 index 0000000..8476b74 --- /dev/null +++ b/6.c @@ -0,0 +1,69 @@ +#include +#include +#include + +#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; +} diff --git a/6a.c b/6a.c new file mode 100644 index 0000000..2c0031d --- /dev/null +++ b/6a.c @@ -0,0 +1,108 @@ +#include +#include +#include + +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; +} diff --git a/data.6 b/data.6 new file mode 100644 index 0000000..6b5d079 --- /dev/null +++ b/data.6 @@ -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 diff --git a/data.6a b/data.6a new file mode 100644 index 0000000..55129f1 --- /dev/null +++ b/data.6a @@ -0,0 +1 @@ +3,4,3,1,2 -- cgit v1.2.3