summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-12-09 00:28:32 -0500
committerJune McEnroe <june@causal.agency>2020-12-09 00:28:32 -0500
commitee31c9b7002c6dd9ea824007611a4899aae9faea (patch)
treee0b1e62bb900941713978dc9e7ba7c8377545c27
parentSolve day 9 part 1 (diff)
downloadaoc-ee31c9b7002c6dd9ea824007611a4899aae9faea.tar.gz
aoc-ee31c9b7002c6dd9ea824007611a4899aae9faea.zip
Solve day 9 part 2
Diffstat (limited to '')
-rw-r--r--2020/day09.c26
1 files changed, 23 insertions, 3 deletions
diff --git a/2020/day09.c b/2020/day09.c
index f67fb4e..5b5d5d4 100644
--- a/2020/day09.c
+++ b/2020/day09.c
@@ -1,3 +1,4 @@
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 enum { Cap = 25 };
@@ -17,12 +18,31 @@ static int valid(int x) {
 }
 int main(void) {
 	int x;
+	int list[1024];
+	int len = 0;
 	while (EOF != scanf("%d\n", &x)) {
-		if (index < Cap || valid(x)) {
-			push(x);
+		list[len++] = x;
+	}
+	int invalid = 0;
+	for (int i = 0; i < len; ++i) {
+		if (index < Cap || valid(list[i])) {
+			push(list[i]);
 		} else {
-			printf("%d\n", x);
+			printf("%d\n", list[i]);
+			invalid = list[i];
 			break;
 		}
 	}
+	for (int i = 0; i < len; ++i) {
+		int sum = 0, min = INT_MAX, max = INT_MIN;
+		for (int j = i; j < len; ++j) {
+			if (list[j] < min) min = list[j];
+			if (list[j] > max) max = list[j];
+			sum += list[j];
+			if (sum == invalid) {
+				printf("%d\n", min + max);
+				return 0;
+			}
+		}
+	}
 }