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;
			}
		}
	}
}
gnore ".dirty" suffix on Git versionJohn Keeping 2013-04-15tests: set TEST_OUTPUT_DIRECTORY to the CGit test directoryJohn Keeping 2013-04-15t0109: test more URLsJohn Keeping 2013-04-10cgitrc.5.txt: Specify when scan-path must be defined before.Jason A. Donenfeld 2013-04-10ui-snapshot.c: Prepend "V" when guessing ref namesLukas Fleischer 2013-04-10t0107: Skip ZIP tests if unzip(1) isn't availableLukas Fleischer 2013-04-10tests/: Do not use `sed -i`Lukas Fleischer 2013-04-10Add branch-sort and repo.branch-sort options.Jason A. Donenfeld 2013-04-10t0109: chain operations with &&John Keeping 2013-04-10cgit.c: Do not restore unset environment variablesLukas Fleischer 2013-04-09t0107: Use `tar -z` for gzip'ed archivesLukas Fleischer