day 6 part 2 (C). Implies change initial stupid algorithm with simple one

This commit is contained in:
2021-12-08 21:37:25 +01:00
parent 6a0a81aa38
commit 7c981d6633
3 changed files with 57 additions and 67 deletions

View File

@@ -68,3 +68,14 @@ aoc-c : res=20500
time: 0:00.00 real, 0.00 user, 0.00 sys time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+1054 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

View File

@@ -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? Find a way to simulate lanternfish. How many lanternfish would there be after 80 days?
Your puzzle answer was 362666. Your puzzle answer was 362666.
The first half of this puzzle is complete! It provides one gold star: *
--- Part Two --- --- Part Two ---
Suppose the lanternfish live forever and have unlimited food and space. Would they take over the entire ocean? 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! After 256 days in the example above, there would be a total of 26984457539 lanternfish!
How many lanternfish would there be after 256 days? 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: **

View File

@@ -18,104 +18,81 @@
#include "debug.h" #include "debug.h"
#include "bits.h" #include "bits.h"
#include "list.h"
#include "pool.h"
typedef struct fish { #define NFISH 9
s16 value; static u64 fish[NFISH];
struct list_head list;
} fish_t;
static pool_t *pool; inline static u64 fish_count()
{
u64 nfish = 0;
LIST_HEAD(fish_head); for (int i = 0; i < NFISH; ++i)
static int nfish=0; nfish += fish[i];
return nfish;
}
//#ifdef DEBUG #ifdef DEBUG
static void print_fish() static void print_fish()
{ {
fish_t *fish;
int i = 0; int i = 0;
printf("fish # = %d\n", nfish); printf("fish: %lu\n", fish_count());
list_for_each_entry(fish, &fish_head, list) { for (i = 0; i < NFISH; ++i)
printf("%s%d", i? ",":"", fish->value); printf("%10d", i);
i++; printf("\n");
} for (i = 0; i < NFISH; ++i)
printf("%10lu", fish[i]);
printf("\n"); printf("\n");
} }
//#endif #endif
static struct list_head *read_fish() static u64 read_fish()
{ {
char *buf, *token; char *buf, *token;
size_t alloc = 0; size_t alloc = 0;
fish_t *fish; u64 nfish = 0;
if (getline(&buf, &alloc, stdin) < 0) if (getline(&buf, &alloc, stdin) < 0)
return NULL; return -1;
if (!(pool = pool_init("fish", 1024, sizeof (struct fish))))
return NULL;
token = strtok(buf, ",\n"); token = strtok(buf, ",\n");
while (token) { while (token) {
//printf("token=[%s]\n", token); fish[atoi(token)]++;
if (!(fish = pool_get(pool))) nfish++;
return NULL;
fish->value = atoi(token);
list_add_tail(&fish->list, &fish_head);
nfish ++;
token = strtok(NULL, ",\n"); token = strtok(NULL, ",\n");
//print_fish();
} }
free(buf); free(buf);
return &fish_head; return nfish;
} }
static int doit(int part) static void doit(int part, int iter)
{ {
fish_t *fish, *new; u64 toadd;
int toadd;
int iter = part == 1? 80: 256; 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) { for (; iter; --iter) {
//printf("iter = %2d: ", iter); toadd = fish[0];
toadd = 0; for (int i = 1; i < NFISH; ++i)
list_for_each_entry(fish, &fish_head, list) { fish[i - 1] = fish[i];
fish->value--; fish[NFISH - 1] = toadd;
if (fish->value < 0) { /* was zero: create new fish */ fish[6] += toadd;
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) 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; return 1;
} }
int main(int ac, char **av) int main(int ac, char **av)
{ {
int opt; int opt, iter = 0;
u32 exercise = 1, res; u32 exercise = 1;
while ((opt = getopt(ac, av, "d:p:")) != -1) { while ((opt = getopt(ac, av, "d:p:i:")) != -1) {
switch (opt) { switch (opt) {
case 'd': case 'd':
debug_level_set(atoi(optarg)); debug_level_set(atoi(optarg));
@@ -125,6 +102,9 @@ int main(int ac, char **av)
if (exercise < 1 || exercise > 2) if (exercise < 1 || exercise > 2)
return usage(*av); return usage(*av);
break; break;
case 'i': /* 1 or 2 */
iter = atoi(optarg);
break;
default: default:
return usage(*av); return usage(*av);
} }
@@ -133,10 +113,7 @@ int main(int ac, char **av)
return usage(*av); return usage(*av);
read_fish(); read_fish();
print_fish(); doit(exercise, iter);
res = doit(exercise); printf ("%s : res=%lu\n", *av, fish_count());
//print_fish();
printf ("%s : res=%d\n", *av, res);
/* TODO: free board/mem pool */
exit (0); exit (0);
} }