repositories
/
advent21.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
46b95a2
)
speed up
author
Mart Lubbers
<mart@martlubbers.net>
Mon, 6 Dec 2021 08:34:56 +0000
(09:34 +0100)
committer
Mart Lubbers
<mart@martlubbers.net>
Mon, 6 Dec 2021 08:34:56 +0000
(09:34 +0100)
06a.c
patch
|
blob
|
history
06b.c
patch
|
blob
|
history
diff --git
a/06a.c
b/06a.c
index
92a1b8a
..
c9152c6
100644
(file)
--- a/
06a.c
+++ b/
06a.c
@@
-7,28
+7,17
@@
int main()
char buf[1000];
if (fgets(buf, 1000, stdin) == NULL)
return 1;
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, ",");
char *p = strtok(buf, ",");
-
this
[atoi(p)]++;
+
fish
[atoi(p)]++;
while ((p = strtok(NULL, ",")) != NULL)
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++)
long sum = 0;
for (long i = 0; i<=8; i++)
- sum +=
this
[i];
+ sum +=
fish
[i];
printf("%lu\n", sum);
}
printf("%lu\n", sum);
}
diff --git
a/06b.c
b/06b.c
index
abc9892
..
0b2f076
100644
(file)
--- a/
06b.c
+++ b/
06b.c
@@
-7,28
+7,17
@@
int main()
char buf[1000];
if (fgets(buf, 1000, stdin) == NULL)
return 1;
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, ",");
char *p = strtok(buf, ",");
-
this
[atoi(p)]++;
+
fish
[atoi(p)]++;
while ((p = strtok(NULL, ",")) != NULL)
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++)
long sum = 0;
for (long i = 0; i<=8; i++)
- sum +=
this
[i];
+ sum +=
fish
[i];
printf("%lu\n", sum);
}
printf("%lu\n", sum);
}