aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-12-10 18:32:06 +0100
committerMarvin Borner2020-12-10 18:32:06 +0100
commit15cc7bd34fa86d6a3f6dadfce0bf5db914670927 (patch)
treef55d08f818e9ab00f620890d91c39832a4d291ad
parenta31e759755fe8b6cc4ab7e18cacb7e36265ae191 (diff)
Cleaned up
-rw-r--r--2020/10/solve.c20
1 files changed, 10 insertions, 10 deletions
diff --git a/2020/10/solve.c b/2020/10/solve.c
index 83c5535..0eb8970 100644
--- a/2020/10/solve.c
+++ b/2020/10/solve.c
@@ -20,16 +20,17 @@ int part_one(int *data)
return one * three;
}
+#define COND(i, o) ((i) + (o) < LENGTH && *(cur + (o)) - *cur <= 3)
long part_two(int *data)
{
- long a, b, c;
- a = 1;
- b = 0;
- c = 0;
- for (int i = LENGTH - 2; i > 0; i--) {
- long next = (i + 1 < LENGTH && data[i + 1] - data[i] <= 3 ? a : 0) +
- (i + 2 < LENGTH && data[i + 2] - data[i] <= 3 ? b : 0) +
- (i + 3 < LENGTH && data[i + 3] - data[i] <= 3 ? c : 0);
+ long a = 1;
+ long b = 0;
+ long c = 0;
+
+ int *cur = &data[LENGTH - 1];
+ while (cur-- != &data[0]) {
+ int i = cur - data;
+ long next = (COND(i, 1) ? a : 0) + (COND(i, 2) ? b : 0) + (COND(i, 3) ? c : 0);
c = b;
b = a;
a = next;
@@ -56,7 +57,7 @@ int *to_int(FILE *fp, int *arr)
int d = 0, i = 0;
while (fscanf(fp, "%d\n", &d) != EOF)
arr[i++] = d;
- LENGTH = i + 2;
+ LENGTH = i + 1;
return arr;
}
@@ -70,7 +71,6 @@ int main()
int data[128] = { 0 };
to_int(fp, data);
qsort(data, LENGTH, sizeof(int), cmp);
- data[LENGTH] = data[LENGTH - 1] + 3;
printf("%d\n", part_one(data));
printf("%lu\n", part_two(data));