repositories
/
advent21.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
day 14 part twoq
[advent21.git]
/
06b.c
diff --git
a/06b.c
b/06b.c
index
0b2f076
..
bd2e220
100644
(file)
--- a/
06b.c
+++ b/
06b.c
@@
-2,22
+2,32
@@
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
+#include <gmp.h>
+
int main()
{
int main()
{
- char buf[1000];
- if (fgets(buf, 1000, stdin) == NULL)
- return 1;
- long fish[9] = {0};
+ char *buf = NULL;
+ size_t len = 0;
+ mpz_t fish[9];
+ for (int i = 0; i<9; i++)
+ mpz_init_set_ui(fish[i], 0);
+
+ getline(&buf, &len, stdin);
char *p = strtok(buf, ",");
char *p = strtok(buf, ",");
- fish[atoi(p)]++;
- while ((p = strtok(NULL, ",")) != NULL)
- fish[atoi(p)]++;
+ int n = atoi(p);
+ mpz_add_ui(fish[n], fish[n], 1);
+ while ((p = strtok(NULL, ",")) != NULL) {
+ n = atoi(p);
+ mpz_add_ui(fish[n], fish[n], 1);
+ }
- for (long day = 0; day<256; day++)
- fish[(day+7) % 9] += fish[day%9];
+ for (long day = 0; day<256; day++) {
+ n = (day+7) % 9;
+ mpz_add(fish[n], fish[n], fish[day%9]);
+ }
- long sum = 0;
- for (long i = 0; i<=8; i++)
-
sum += fish[i]
;
- p
rintf("%lu\n", sum
);
+ for (long i = 1; i<=8; i++)
+ mpz_add(fish[0], fish[0], fish[i]);
+
mpz_out_str(stdout, 10, fish[0])
;
+ p
utchar('\n'
);
}
}