diff options
-rw-r--r-- | 2020/day09.c | 26 |
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; + } + } + } } |