From 2d015db00d7612786a98db0cdfda291b6dc08389 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Mon, 6 Dec 2021 09:34:56 +0100 Subject: [PATCH] speed up --- 06a.c | 23 ++++++----------------- 06b.c | 23 ++++++----------------- 2 files changed, 12 insertions(+), 34 deletions(-) diff --git a/06a.c b/06a.c index 92a1b8a..c9152c6 100644 --- a/06a.c +++ b/06a.c @@ -7,28 +7,17 @@ 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]; - + long fish[9] = {0}; 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]; + sum += fish[i]; printf("%lu\n", sum); } diff --git a/06b.c b/06b.c index abc9892..0b2f076 100644 --- a/06b.c +++ b/06b.c @@ -7,28 +7,17 @@ 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]; - + long fish[9] = {0}; 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<256; 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<256; day++) + fish[(day+7) % 9] += fish[day%9]; long sum = 0; for (long i = 0; i<=8; i++) - sum += this[i]; + sum += fish[i]; printf("%lu\n", sum); } -- 2.20.1