X-Git-Url: https://git.martlubbers.net/?a=blobdiff_plain;f=06a.c;h=e2634d21ba73e026ecccfd2f7be314b99e793a36;hb=967152a3319e0207e16901ecc0344edaf8c8193e;hp=92a1b8ae972e27c0efd07b91e2adaef3c4472633;hpb=46b95a2f1d484eed878202f204561932dd9c548b;p=advent21.git diff --git a/06a.c b/06a.c index 92a1b8a..e2634d2 100644 --- a/06a.c +++ b/06a.c @@ -4,31 +4,20 @@ int main() { - char buf[1000]; - if (fgets(buf, 1000, stdin) == NULL) - return 1; - long fish0[9] = {0}; - long fish1[9] = {0}; - long *this = &fish0[0]; - long *that = &fish1[0]; + char *buf = NULL; + size_t len = 0; + long fish[9] = {0}; + getline(&buf, &len, stdin); char *p = strtok(buf, ","); - this[atoi(p)]++; + fish[atoi(p)]++; while ((p = strtok(NULL, ",")) != NULL) - this[atoi(p)]++; + fish[atoi(p)]++; - for (long day = 0; day<80; day++) { - that[8] = this[0]; - for (long i = 0; i<8; i++) - that[i] = this[i+1]; - that[6] += this[0]; - long *tmp = this; - this = that; - that = tmp; - } + for (long day = 0; day<80; day++) + fish[(day+7) % 9] += fish[day%9]; - long sum = 0; - for (long i = 0; i<=8; i++) - sum += this[i]; - printf("%lu\n", sum); + for (long i = 1; i<=8; i++) + fish[0] += fish[i]; + printf("%ld\n", fish[0]); }