From 554ad47f09974f0131df285fda8ea50a1fdad8f5 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Mon, 11 Dec 2023 15:14:47 +0100 Subject: Initial commit, some solutions --- 2023/09/9a.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 2023/09/9a.c (limited to '2023/09/9a.c') diff --git a/2023/09/9a.c b/2023/09/9a.c new file mode 100644 index 0000000..10b7beb --- /dev/null +++ b/2023/09/9a.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include + +#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; +} -- cgit v1.3