aboutsummaryrefslogtreecommitdiff
path: root/2023/09/9a.c
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2023-12-11 15:14:47 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2023-12-11 15:14:47 +0100
commit554ad47f09974f0131df285fda8ea50a1fdad8f5 (patch)
treed23caeb85e7fce6290664a0ebf3c96f513efbe94 /2023/09/9a.c
downloadaoc-554ad47f09974f0131df285fda8ea50a1fdad8f5.tar.gz
aoc-554ad47f09974f0131df285fda8ea50a1fdad8f5.zip
Initial commit, some solutions
Diffstat (limited to '2023/09/9a.c')
-rw-r--r--2023/09/9a.c46
1 files changed, 46 insertions, 0 deletions
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 @@
1#include <inttypes.h>
2#include <stdbool.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6
7#define N 1000
8
9#define isnum(c) (c == '-' || (c >= '0' && c <= '9'))
10
11int64_t readl(int64_t nums[], char *buf) {
12 int64_t i;
13 for (i = 0; *buf; buf++) {
14 if (!isnum(*buf)) continue;
15 nums[i++] = atoll(buf);
16 while (isnum(*buf)) buf++;
17 }
18 return i;
19}
20
21bool isconstant(int64_t a[], int n) {
22 for (int i = 0; i < n-1; i++)
23 if (a[i] != a[i+1])
24 return false;
25 return true;
26}
27
28int64_t next(int64_t a[], int64_t n) {
29 int64_t x = a[n-1];
30 if (isconstant(a, n))
31 return x;
32 for (int i = 0; i < n-1; i++)
33 a[i] = a[i+1] - a[i];
34 return x + next(a, n-1);
35}
36
37int main() {
38 char line[N];
39 int64_t s, a[N];
40
41 for (s = 0; fgets(line, N, stdin) != NULL; )
42 s += next(a, readl(a, line));
43
44 printf("%" PRId64 "\n", s);
45 return 0;
46}

Generated with cgit - Back to sebastiano.tronto.net