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 /6.c | |
| parent | e34fd16b64ebf0ddd7ede60e938935320135d3ee (diff) | |
Added day 6. 6a needs some optimisation, or a lote of memory
Diffstat (limited to '6.c')
| -rw-r--r-- | 6.c | 69 |
1 files changed, 69 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; +} |
