commit 01e91e73f0dd2caaffbda55b347731505f566efb
parent 8d6c90c171aa8ab3713367cbe9c3b1436f590a70
Author: bsandro <email@bsandro.tech>
Date: Wed, 17 Jan 2024 17:55:53 +0200
day 09 p2
Diffstat:
1 file changed, 15 insertions(+), 9 deletions(-)
diff --git a/day09/puzzle.c b/day09/puzzle.c
@@ -44,13 +44,21 @@ int IntArray_Diff(IntArray *a) {
for (int i=1; i<a->len; ++i) {
IntArray_Add(&n, a->values[i] - a->values[i-1]);
}
- //printf("diff array:\n");
- //IntArray_Print(&n);
int diff = IntArray_Diff(&n);
- //printf("d=%d\n", diff);
return a->values[a->len-1]+diff;
}
+int IntArray_Rdiff(IntArray *a) {
+ if (IntArray_Same(a)) return a->values[0];
+ if (a->len==1) { printf("error: array of len 1\n"); exit(-1); }
+ IntArray n = {0};
+ for (int i=0; i<a->len-1; ++i) {
+ IntArray_Add(&n, a->values[i+1] - a->values[i]);
+ }
+ int diff = IntArray_Rdiff(&n);
+ return a->values[0]-diff;
+}
+
void puzzle(const char *filename, long long *result1, long long *result2) {
FILE *infile = fopen(filename, "r");
if (infile == NULL) {
@@ -71,13 +79,11 @@ void puzzle(const char *filename, long long *result1, long long *result2) {
}
free(s);
- /*printf("----- readings: -----\n");
- IntArray_Print(&readings);
- printf("---------------------\n");*/
+ int diff1 = IntArray_Diff(&readings);
+ *result1 += diff1;
- int diff = IntArray_Diff(&readings);
- //printf("diffs:%d\n", diff);
- *result1 += diff;
+ int diff2 = IntArray_Rdiff(&readings);
+ *result2 += diff2;
//@todo list_free ?
++line_num;
bzero(buf, STR_LEN);