diff --git a/2021/day06/EXAMPLE.txt b/2021/day06/EXAMPLE.txt index 9099164..55129f1 100644 --- a/2021/day06/EXAMPLE.txt +++ b/2021/day06/EXAMPLE.txt @@ -1 +1 @@ -Initial state: 3,4,3,1,2 +3,4,3,1,2 diff --git a/2021/day06/README.txt b/2021/day06/README.txt index bcd8a54..af9f3b5 100644 --- a/2021/day06/README.txt +++ b/2021/day06/README.txt @@ -51,3 +51,14 @@ Each day, a 0 becomes a 6 and adds a new 8 to the end of the list, while each ot In this example, after 18 days, there are a total of 26 fish. After 80 days, there would be a total of 5934. 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? + +After 256 days in the example above, there would be a total of 26984457539 lanternfish! + +How many lanternfish would there be after 256 days? diff --git a/2021/day06/aoc-c.c b/2021/day06/aoc-c.c new file mode 100644 index 0000000..7430bed --- /dev/null +++ b/2021/day06/aoc-c.c @@ -0,0 +1,142 @@ +/* aoc-c: Advent2021 game, day 6 parts 1 & 2 + * + * Copyright (C) 2021 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include +#include +#include +#include +#include + +#include "debug.h" +#include "bits.h" +#include "list.h" +#include "pool.h" + +typedef struct fish { + s16 value; + struct list_head list; +} fish_t; + +static pool_t *pool; + +LIST_HEAD(fish_head); +static int nfish=0; + +//#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("\n"); +} +//#endif + +static struct list_head *read_fish() +{ + char *buf, *token; + size_t alloc = 0; + fish_t *fish; + + if (getline(&buf, &alloc, stdin) < 0) + return NULL; + + if (!(pool = pool_init("fish", 1024, sizeof (struct fish)))) + return NULL; + + 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 ++; + token = strtok(NULL, ",\n"); + //print_fish(); + } + free(buf); + return &fish_head; +} + +static int doit(int part) +{ + fish_t *fish, *new; + int toadd; + int 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); + } + } + return nfish; +} + +static int usage(char *prg) +{ + fprintf(stderr, "Usage: %s [-d debug_level] [-p part]\n", prg); + return 1; +} + +int main(int ac, char **av) +{ + int opt; + u32 exercise = 1, res; + + while ((opt = getopt(ac, av, "d:p:")) != -1) { + switch (opt) { + case 'd': + debug_level_set(atoi(optarg)); + break; + case 'p': /* 1 or 2 */ + exercise = atoi(optarg); + if (exercise < 1 || exercise > 2) + return usage(*av); + break; + default: + return usage(*av); + } + } + if (optind < ac) + return usage(*av); + + read_fish(); + print_fish(); + res = doit(exercise); + //print_fish(); + printf ("%s : res=%d\n", *av, res); + /* TODO: free board/mem pool */ + exit (0); +}