diff options
Diffstat (limited to '2023/09/9b.c')
| -rw-r--r-- | 2023/09/9b.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/2023/09/9b.c b/2023/09/9b.c new file mode 100644 index 0000000..1b539c1 --- /dev/null +++ b/2023/09/9b.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 | |||
| 11 | int64_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 | |||
| 21 | bool 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 | |||
| 28 | int64_t next(int64_t a[], int64_t n) { | ||
| 29 | int64_t x = a[0]; | ||
| 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 | |||
| 37 | int 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 | } | ||
