aboutsummaryrefslogtreecommitdiff
path: root/2023/09/9a.c
blob: 741c344c516f939bca15d776defa43319a8156b7 (plain)
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
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#define N 1000

#define isnum(c) (c == '-' || (c >= '0' && c <= '9'))

int64_t readl(int64_t nums[], char *buf) {
	int64_t i;
	for (i = 0; *buf; buf++) {
		if (!isnum(*buf)) continue;
		nums[i++] = atoll(buf);
		while (isnum(*buf)) buf++;
	}
	return i;
}

bool isconstant(int64_t a[], int n) {
	for (int i = 0; i < n-1; i++)
		if (a[i] != a[i+1])
			return false;
	return true;
}

int64_t next(int64_t a[], int64_t n) {
	int64_t x = a[n-1];
	if (isconstant(a, n))
		return x;
	for (int i = 0; i < n-1; i++)
		a[i] = a[i+1] - a[i];
	return x + next(a, n-1);
}

int main() {
	char line[N];
	int64_t s, a[N];

	for (s = 0; fgets(line, N, stdin) != NULL; )
		s += next(a, readl(a, line));

	printf("%" PRId64 "\n", s);
	return 0;
}

Generated with cgit - Back to sebastiano.tronto.net