summary refs log tree commit diff homepage
path: root/2020/day09.c
blob: 5b5d5d41f72336c0dfc6215b87398dd87bf93a31 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
enum { Cap = 25 };
static int ring[Cap];
static size_t index;
static void push(int x) {
	ring[index++ % Cap] = x;
}
static int valid(int x) {
	for (int i = 0; i < Cap; ++i) {
		for (int j = 0; j < Cap; ++j) {
			if (ring[i] == ring[j]) continue;
			if (ring[i] + ring[j] == x) return 1;
		}
	}
	return 0;
}
int main(void) {
	int x;
	int list[1024];
	int len = 0;
	while (EOF != scanf("%d\n", &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", 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;
			}
		}
	}
}