From 7c981d6633db620217806c379188098b20945ce9 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Wed, 8 Dec 2021 21:37:25 +0100 Subject: [PATCH] day 6 part 2 (C). Implies change initial stupid algorithm with simple one --- 2021/OUTPUT | 11 +++++ 2021/day06/README.txt | 6 ++- 2021/day06/aoc-c.c | 107 +++++++++++++++++------------------------- 3 files changed, 57 insertions(+), 67 deletions(-) diff --git a/2021/OUTPUT b/2021/OUTPUT index adf1196..20054f2 100644 --- a/2021/OUTPUT +++ b/2021/OUTPUT @@ -68,3 +68,14 @@ aoc-c : res=20500 time: 0:00.00 real, 0.00 user, 0.00 sys context-switch: 0+1, page-faults: 0+1054 +========================================= +================= day06 ================= +========================================= + +aoc-c : res=362666 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+90 + +aoc-c : res=1640526601595 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+84 diff --git a/2021/day06/README.txt b/2021/day06/README.txt index af9f3b5..57a8796 100644 --- a/2021/day06/README.txt +++ b/2021/day06/README.txt @@ -53,8 +53,6 @@ In this example, after 18 days, there are a total of 26 fish. After 80 days, the Find a way to simulate lanternfish. How many lanternfish would there be after 80 days? Your puzzle answer was 362666. - -The first half of this puzzle is complete! It provides one gold star: * --- Part Two --- Suppose the lanternfish live forever and have unlimited food and space. Would they take over the entire ocean? @@ -62,3 +60,7 @@ Suppose the lanternfish live forever and have unlimited food and space. Would th After 256 days in the example above, there would be a total of 26984457539 lanternfish! How many lanternfish would there be after 256 days? + +Your puzzle answer was 1640526601595. + +Both parts of this puzzle are complete! They provide two gold stars: ** diff --git a/2021/day06/aoc-c.c b/2021/day06/aoc-c.c index 7430bed..b41205d 100644 --- a/2021/day06/aoc-c.c +++ b/2021/day06/aoc-c.c @@ -18,104 +18,81 @@ #include "debug.h" #include "bits.h" -#include "list.h" -#include "pool.h" -typedef struct fish { - s16 value; - struct list_head list; -} fish_t; +#define NFISH 9 +static u64 fish[NFISH]; -static pool_t *pool; +inline static u64 fish_count() +{ + u64 nfish = 0; -LIST_HEAD(fish_head); -static int nfish=0; + for (int i = 0; i < NFISH; ++i) + nfish += fish[i]; + return nfish; +} -//#ifdef DEBUG +#ifdef DEBUG static void print_fish() { - fish_t *fish; int i = 0; - printf("fish # = %d\n", nfish); - list_for_each_entry(fish, &fish_head, list) { - printf("%s%d", i? ",":"", fish->value); - i++; - } + printf("fish: %lu\n", fish_count()); + for (i = 0; i < NFISH; ++i) + printf("%10d", i); + printf("\n"); + for (i = 0; i < NFISH; ++i) + printf("%10lu", fish[i]); printf("\n"); } -//#endif +#endif -static struct list_head *read_fish() +static u64 read_fish() { char *buf, *token; size_t alloc = 0; - fish_t *fish; + u64 nfish = 0; if (getline(&buf, &alloc, stdin) < 0) - return NULL; - - if (!(pool = pool_init("fish", 1024, sizeof (struct fish)))) - return NULL; + return -1; token = strtok(buf, ",\n"); while (token) { - //printf("token=[%s]\n", token); - if (!(fish = pool_get(pool))) - return NULL; - fish->value = atoi(token); - list_add_tail(&fish->list, &fish_head); - nfish ++; + fish[atoi(token)]++; + nfish++; token = strtok(NULL, ",\n"); - //print_fish(); } free(buf); - return &fish_head; + return nfish; } -static int doit(int part) +static void doit(int part, int iter) { - fish_t *fish, *new; - int toadd; - int iter = part == 1? 80: 256; + u64 toadd; + + if (!iter) + iter = part == 1 ? 80: 256; - /* initial algorithm surely does not work for part 2: - * Too many memory allocation, nneed to rethink the whole... - * - */ for (; iter; --iter) { - //printf("iter = %2d: ", iter); - toadd = 0; - list_for_each_entry(fish, &fish_head, list) { - fish->value--; - if (fish->value < 0) { /* was zero: create new fish */ - fish->value = 6; - toadd++; - nfish++; - } - } - while (toadd--) { - if (!(new = pool_get(pool))) - return -1; - new->value = 8; - list_add_tail(&new->list, &fish_head); - } + toadd = fish[0]; + for (int i = 1; i < NFISH; ++i) + fish[i - 1] = fish[i]; + fish[NFISH - 1] = toadd; + fish[6] += toadd; } - return nfish; } static int usage(char *prg) { - fprintf(stderr, "Usage: %s [-d debug_level] [-p part]\n", prg); + fprintf(stderr, "Usage: %s [-d debug_level] [-p part] [-i iter]\n", prg); return 1; } int main(int ac, char **av) { - int opt; - u32 exercise = 1, res; + int opt, iter = 0; + u32 exercise = 1; - while ((opt = getopt(ac, av, "d:p:")) != -1) { + while ((opt = getopt(ac, av, "d:p:i:")) != -1) { switch (opt) { case 'd': debug_level_set(atoi(optarg)); @@ -125,6 +102,9 @@ int main(int ac, char **av) if (exercise < 1 || exercise > 2) return usage(*av); break; + case 'i': /* 1 or 2 */ + iter = atoi(optarg); + break; default: return usage(*av); } @@ -133,10 +113,7 @@ int main(int ac, char **av) return usage(*av); read_fish(); - print_fish(); - res = doit(exercise); - //print_fish(); - printf ("%s : res=%d\n", *av, res); - /* TODO: free board/mem pool */ + doit(exercise, iter); + printf ("%s : res=%lu\n", *av, fish_count()); exit (0); }